import numpy as np
import pandas as pd
import warnings
from scipy.stats import pearsonr
from tqdm import tqdm
warnings.filterwarnings('ignore')
# reminder that if you are installing libraries in a Google Colab instance you will be prompted to restart your kernal
try:
import geemap, ee
import seaborn as sns
import matplotlib.izingpyplot as plt
except ModuleNotFoundError:
if 'google.colab' in str(get_ipython()):
print("package not found, installing w/ pip in Google Colab...")
!pip install geemap seaborn matplotlib
else:
print("package not found, installing w/ conda...")
!conda install mamba -c conda-forge -y
!mamba install geemap -c conda-forge -y
!conda install seaborn matplotlib -y
import geemap, ee
import seaborn as sns
import matplotlib.pyplot as plt
package not found, installing w/ pip in Google Colab... Requirement already satisfied: geemap in /usr/local/lib/python3.10/dist-packages (0.28.2) Requirement already satisfied: seaborn in /usr/local/lib/python3.10/dist-packages (0.12.2) Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (3.7.1) Requirement already satisfied: bqplot in /usr/local/lib/python3.10/dist-packages (from geemap) (0.12.42) Requirement already satisfied: colour in /usr/local/lib/python3.10/dist-packages (from geemap) (0.1.5) Requirement already satisfied: earthengine-api>=0.1.347 in /usr/local/lib/python3.10/dist-packages (from geemap) (0.1.377) Requirement already satisfied: eerepr>=0.0.4 in /usr/local/lib/python3.10/dist-packages (from geemap) (0.0.4) Requirement already satisfied: folium>=0.13.0 in /usr/local/lib/python3.10/dist-packages (from geemap) (0.14.0) Requirement already satisfied: geocoder in /usr/local/lib/python3.10/dist-packages (from geemap) (1.38.1) Requirement already satisfied: ipyevents in /usr/local/lib/python3.10/dist-packages (from geemap) (2.0.2) Requirement already satisfied: ipyfilechooser>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from geemap) (0.6.0) Requirement already satisfied: ipyleaflet>=0.17.0 in /usr/local/lib/python3.10/dist-packages (from geemap) (0.17.4) Requirement already satisfied: ipytree in /usr/local/lib/python3.10/dist-packages (from geemap) (0.2.2) Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from geemap) (1.23.5) Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from geemap) (1.5.3) Requirement already satisfied: plotly in /usr/local/lib/python3.10/dist-packages (from geemap) (5.15.0) Requirement already satisfied: pyperclip in /usr/local/lib/python3.10/dist-packages (from geemap) (1.8.2) Requirement already satisfied: pyshp>=2.1.3 in /usr/local/lib/python3.10/dist-packages (from geemap) (2.3.1) Requirement already satisfied: python-box in /usr/local/lib/python3.10/dist-packages (from geemap) (7.1.1) Requirement already satisfied: scooby in /usr/local/lib/python3.10/dist-packages (from geemap) (0.9.2) Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.2.0) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (4.44.0) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.4.5) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (23.2) Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (9.4.0) Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (3.1.1) Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (2.8.2) Requirement already satisfied: google-cloud-storage in /usr/local/lib/python3.10/dist-packages (from earthengine-api>=0.1.347->geemap) (2.8.0) Requirement already satisfied: google-api-python-client>=1.12.1 in /usr/local/lib/python3.10/dist-packages (from earthengine-api>=0.1.347->geemap) (2.84.0) Requirement already satisfied: google-auth>=1.4.1 in /usr/local/lib/python3.10/dist-packages (from earthengine-api>=0.1.347->geemap) (2.17.3) Requirement already satisfied: google-auth-httplib2>=0.0.3 in /usr/local/lib/python3.10/dist-packages (from earthengine-api>=0.1.347->geemap) (0.1.1) Requirement already satisfied: httplib2<1dev,>=0.9.2 in /usr/local/lib/python3.10/dist-packages (from earthengine-api>=0.1.347->geemap) (0.22.0) Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from earthengine-api>=0.1.347->geemap) (2.31.0) Requirement already satisfied: branca>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from folium>=0.13.0->geemap) (0.7.0) Requirement already satisfied: jinja2>=2.9 in /usr/local/lib/python3.10/dist-packages (from folium>=0.13.0->geemap) (3.1.2) Requirement already satisfied: ipywidgets in /usr/local/lib/python3.10/dist-packages (from ipyfilechooser>=0.6.0->geemap) (7.7.1) Requirement already satisfied: traittypes<3,>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from ipyleaflet>=0.17.0->geemap) (0.2.1) Requirement already satisfied: xyzservices>=2021.8.1 in /usr/local/lib/python3.10/dist-packages (from ipyleaflet>=0.17.0->geemap) (2023.10.1) Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->geemap) (2023.3.post1) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib) (1.16.0) Requirement already satisfied: traitlets>=4.3.0 in /usr/local/lib/python3.10/dist-packages (from bqplot->geemap) (5.7.1) Requirement already satisfied: click in /usr/local/lib/python3.10/dist-packages (from geocoder->geemap) (8.1.7) Requirement already satisfied: future in /usr/local/lib/python3.10/dist-packages (from geocoder->geemap) (0.18.3) Requirement already satisfied: ratelim in /usr/local/lib/python3.10/dist-packages (from geocoder->geemap) (0.1.6) Requirement already satisfied: tenacity>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from plotly->geemap) (8.2.3) Requirement already satisfied: google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5 in /usr/local/lib/python3.10/dist-packages (from google-api-python-client>=1.12.1->earthengine-api>=0.1.347->geemap) (2.11.1) Requirement already satisfied: uritemplate<5,>=3.0.1 in /usr/local/lib/python3.10/dist-packages (from google-api-python-client>=1.12.1->earthengine-api>=0.1.347->geemap) (4.1.1) Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth>=1.4.1->earthengine-api>=0.1.347->geemap) (5.3.2) Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth>=1.4.1->earthengine-api>=0.1.347->geemap) (0.3.0) Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth>=1.4.1->earthengine-api>=0.1.347->geemap) (4.9) Requirement already satisfied: ipykernel>=4.5.1 in /usr/local/lib/python3.10/dist-packages (from ipywidgets->ipyfilechooser>=0.6.0->geemap) (5.5.6) Requirement already satisfied: ipython-genutils~=0.2.0 in /usr/local/lib/python3.10/dist-packages (from ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.2.0) Requirement already satisfied: widgetsnbextension~=3.6.0 in /usr/local/lib/python3.10/dist-packages (from ipywidgets->ipyfilechooser>=0.6.0->geemap) (3.6.6) Requirement already satisfied: ipython>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from ipywidgets->ipyfilechooser>=0.6.0->geemap) (7.34.0) Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from ipywidgets->ipyfilechooser>=0.6.0->geemap) (3.0.9) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2>=2.9->folium>=0.13.0->geemap) (2.1.3) Requirement already satisfied: google-cloud-core<3.0dev,>=2.3.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-storage->earthengine-api>=0.1.347->geemap) (2.3.3) Requirement already satisfied: google-resumable-media>=2.3.2 in /usr/local/lib/python3.10/dist-packages (from google-cloud-storage->earthengine-api>=0.1.347->geemap) (2.6.0) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->earthengine-api>=0.1.347->geemap) (3.3.2) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->earthengine-api>=0.1.347->geemap) (3.4) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->earthengine-api>=0.1.347->geemap) (2.0.7) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->earthengine-api>=0.1.347->geemap) (2023.7.22) Requirement already satisfied: decorator in /usr/local/lib/python3.10/dist-packages (from ratelim->geocoder->geemap) (4.4.2) Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5->google-api-python-client>=1.12.1->earthengine-api>=0.1.347->geemap) (1.61.0) Requirement already satisfied: protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0.dev0,>=3.19.5 in /usr/local/lib/python3.10/dist-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5->google-api-python-client>=1.12.1->earthengine-api>=0.1.347->geemap) (3.20.3) Requirement already satisfied: google-crc32c<2.0dev,>=1.0 in /usr/local/lib/python3.10/dist-packages (from google-resumable-media>=2.3.2->google-cloud-storage->earthengine-api>=0.1.347->geemap) (1.5.0) Requirement already satisfied: jupyter-client in /usr/local/lib/python3.10/dist-packages (from ipykernel>=4.5.1->ipywidgets->ipyfilechooser>=0.6.0->geemap) (6.1.12) Requirement already satisfied: tornado>=4.2 in /usr/local/lib/python3.10/dist-packages (from ipykernel>=4.5.1->ipywidgets->ipyfilechooser>=0.6.0->geemap) (6.3.2) Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (67.7.2) Requirement already satisfied: jedi>=0.16 in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.19.1) Requirement already satisfied: pickleshare in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.7.5) Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (3.0.39) Requirement already satisfied: pygments in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (2.16.1) Requirement already satisfied: backcall in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.2.0) Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.1.6) Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (4.8.0) Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth>=1.4.1->earthengine-api>=0.1.347->geemap) (0.5.0) Requirement already satisfied: notebook>=4.4.1 in /usr/local/lib/python3.10/dist-packages (from widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (6.5.5) Requirement already satisfied: parso<0.9.0,>=0.8.3 in /usr/local/lib/python3.10/dist-packages (from jedi>=0.16->ipython>=4.0.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.8.3) Requirement already satisfied: pyzmq<25,>=17 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (23.2.1) Requirement already satisfied: argon2-cffi in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (23.1.0) Requirement already satisfied: jupyter-core>=4.6.1 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (5.5.0) Requirement already satisfied: nbformat in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (5.9.2) Requirement already satisfied: nbconvert>=5 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (6.5.4) Requirement already satisfied: nest-asyncio>=1.5 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (1.5.8) Requirement already satisfied: Send2Trash>=1.8.0 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (1.8.2) Requirement already satisfied: terminado>=0.8.3 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.17.1) Requirement already satisfied: prometheus-client in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.18.0) Requirement already satisfied: nbclassic>=0.4.7 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (1.0.0) Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.10/dist-packages (from pexpect>4.3->ipython>=4.0.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.7.0) Requirement already satisfied: wcwidth in /usr/local/lib/python3.10/dist-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=4.0.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.2.9) Requirement already satisfied: platformdirs>=2.5 in /usr/local/lib/python3.10/dist-packages (from jupyter-core>=4.6.1->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (3.11.0) Requirement already satisfied: jupyter-server>=1.8 in /usr/local/lib/python3.10/dist-packages (from nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (1.24.0) Requirement already satisfied: notebook-shim>=0.2.3 in /usr/local/lib/python3.10/dist-packages (from nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.2.3) Requirement already satisfied: lxml in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (4.9.3) Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (4.11.2) Requirement already satisfied: bleach in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (6.1.0) Requirement already satisfied: defusedxml in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.7.1) Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.4) Requirement already satisfied: jupyterlab-pygments in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.2.2) Requirement already satisfied: mistune<2,>=0.8.1 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.8.4) Requirement already satisfied: nbclient>=0.5.0 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.9.0) Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (1.5.0) Requirement already satisfied: tinycss2 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (1.2.1) Requirement already satisfied: fastjsonschema in /usr/local/lib/python3.10/dist-packages (from nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (2.18.1) Requirement already satisfied: jsonschema>=2.6 in /usr/local/lib/python3.10/dist-packages (from nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (4.19.2) Requirement already satisfied: argon2-cffi-bindings in /usr/local/lib/python3.10/dist-packages (from argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (21.2.0) Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (23.1.0) Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (2023.7.1) Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.30.2) Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.12.0) Requirement already satisfied: anyio<4,>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from jupyter-server>=1.8->nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (3.7.1) Requirement already satisfied: websocket-client in /usr/local/lib/python3.10/dist-packages (from jupyter-server>=1.8->nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (1.6.4) Requirement already satisfied: cffi>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (1.16.0) Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4->nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (2.5) Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-packages (from bleach->nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (0.5.1) Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<4,>=3.1.0->jupyter-server>=1.8->nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (1.3.0) Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<4,>=3.1.0->jupyter-server>=1.8->nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (1.1.3) Requirement already satisfied: pycparser in /usr/local/lib/python3.10/dist-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->ipyfilechooser>=0.6.0->geemap) (2.21)
try:
ee.Initialize()
except Exception as e:
ee.Authenticate()
ee.Initialize()
*** Earth Engine *** Share your feedback by taking our Annual Developer Satisfaction Survey: https://google.qualtrics.com/jfe/form/SV_doiqkQG3NJ1t8IS?source=API
!pip install cmasher
import geocoder
import cmasher as cmr
import geemap.colormaps as cm
Requirement already satisfied: cmasher in /usr/local/lib/python3.10/dist-packages (1.6.3) Requirement already satisfied: colorspacious>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from cmasher) (1.1.2) Requirement already satisfied: e13tools>=0.9.4 in /usr/local/lib/python3.10/dist-packages (from cmasher) (0.9.6) Requirement already satisfied: matplotlib>=2.2.4 in /usr/local/lib/python3.10/dist-packages (from cmasher) (3.7.1) Requirement already satisfied: numpy>=1.16.0 in /usr/local/lib/python3.10/dist-packages (from cmasher) (1.23.5) Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.4->cmasher) (1.2.0) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.4->cmasher) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.4->cmasher) (4.44.0) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.4->cmasher) (1.4.5) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.4->cmasher) (23.2) Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.4->cmasher) (9.4.0) Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.4->cmasher) (3.1.1) Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.4->cmasher) (2.8.2) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib>=2.2.4->cmasher) (1.16.0)
from google.colab import auth
import gspread
from google.auth import default
#autenticating to google
auth.authenticate_user()
creds, _ = default()
gc = gspread.authorize(creds)
import matplotlib.dates as mdates
from matplotlib import cm
from tqdm import tqdm
import itertools
import statsmodels.api as sm
import warnings
warnings.filterwarnings('ignore')
from matplotlib import gridspec
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter
import datetime
import matplotlib.ticker as mtick
from scipy import stats
import numpy as np
import pandas as pd
import warnings
from scipy.stats import pearsonr
# import geocoder
# import geemap.colormaps as cm
# !pip install geopandas
# import geopandas as gpd
from google.colab import drive
drive.mount('/content/gdrive')
Mounted at /content/gdrive
import os
# # gps_path = '/content/gdrive/My Drive/Research - MS/Codes/MS Thesis/MS-Thesis/Shape_Files/PHGE71FL/PHGE71FL.shp'
# # geopath = gpd.read_file(geopath)
# path = '/content/gdrive/My Drive/Research - MS/Codes/MS Thesis/MS-Thesis/Shape_Files/PhilGIS/'
# sf_gpd_country = gpd.read_file(path + "Country (extract.me)/Country.shp")
# sf_gpd_regions = gpd.read_file(path + "Regions (extract.me)/Regions.shp")
# sf_gpd_municities = gpd.read_file(path + "MuniCities/MuniCities.shp")
# sf_gpd_provinces = gpd.read_file(path + "Provinces/Provinces.shp")
# sf_gpd_roads = gpd.read_file(path + "Roads/roads.shp")
# plt.figure(dpi = 200)
# plt.subplot(151)
# sf_gpd_country.plot(edgecolor="black", linewidth = 0.5, facecolor="None", ax = plt.gca())
# plt.axis("off")
# plt.subplot(152)
# sf_gpd_regions.plot(edgecolor="red", linewidth = 0.5, facecolor="None", ax = plt.gca())
# plt.rc('font', size = 5)
# plt.axis("off")
# plt.subplot(153)
# sf_gpd_provinces.plot(edgecolor="orange", linewidth = 0.5, facecolor="None", ax = plt.gca())
# plt.rc('font', size = 5)
# plt.axis("off")
# plt.subplot(154)
# sf_gpd_municities.plot(edgecolor="green", linewidth = 0.5, facecolor="None", ax = plt.gca())
# plt.rc('font', size = 5)
# plt.axis("off")
# plt.subplot(155)
# sf_gpd_roads.plot(edgecolor="blue", linewidth = 0.5, facecolor="None", ax = plt.gca())
# plt.rc('font', size = 5)
# plt.axis("off")
# plt.tight_layout()
time_series_()¶def time_series_(lat, lon, buffer):
def poi_mean(img):
img = img.updateMask(img.gte(0))
mean = img.reduceRegion(reducer=ee.Reducer.mean(), geometry=poi, scale=500).get('avg_rad')
return img.set('date', img.date().format()).set('mean',mean)
poi = ee.Geometry.Point(lat, lon).buffer(buffer)
viirs = ee.ImageCollection("NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG").filterDate('2014-01-01','2023-12-31')
poi_reduced_imgs = viirs.map(poi_mean)
nested_list = poi_reduced_imgs.reduceColumns(ee.Reducer.toList(1), ['mean']).values().get(0)
data = pd.DataFrame(nested_list.getInfo(), columns=['mean'])
data = data['mean'].values.tolist()
return data
def time_series_(lat, lon, buffer):
def poi_mean(img):
img = img.updateMask(img.gte(0))
mean = img.reduceRegion(reducer=ee.Reducer.mean(), geometry=poi, scale=500).get('avg_rad')
return img.set('date', img.date().format()).set('mean',mean)
poi = ee.Geometry.Point(lat, lon).buffer(buffer)
viirs = ee.ImageCollection("NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG").filterDate('2014-01-01','2023-12-31')
poi_reduced_imgs = viirs.map(poi_mean)
nested_list = poi_reduced_imgs.reduceColumns(ee.Reducer.toList(2), ['date', 'mean']).values().get(0)
data = pd.DataFrame(nested_list.getInfo(), columns=['date','mean'])
data = data.set_index('date')
# data = data['mean'].values.tolist()
return data
nested_list = prov_mean.reduceColumns(ee.Reducer.toList(2), ['date','mean']).values().get(0)
nested_list_2 = reg_mean.reduceColumns(ee.Reducer.toList(1), ['mean']).values().get(0)
regional_NTL = np.asarray(nested_list_2.getInfo()).squeeze()
# convert to dataframe
NTL_dataset = pd.DataFrame(nested_list.getInfo(), columns=['date', str(Province) + ' Province'])
NTL_dataset['date'] = pd.to_datetime(NTL_dataset['date'])
NTL_dataset = NTL_dataset.set_index('date')
NTL_dataset.insert(1, str(reg_names[reg_codes.index(Region)]), regional_NTL)
#defining my worksheet
worksheet = gc.open('List of Housing Projects').sheet1
rows = worksheet.get_all_values()
#Convert to a DataFrame
df = pd.DataFrame(rows)
#creating columns name
df.columns = df.iloc[0]
df = df.iloc[1:]
#convert 'strings' to 'float'
df['Latitude'] = pd.to_numeric(df['Latitude'], errors='coerce')
df['Longitude'] = pd.to_numeric(df['Longitude'], errors='coerce')
df['Buffer'] = pd.to_numeric(df['Buffer'], errors='coerce')
# df['Latitude'] = df['Latitude'].fillna(0).astype(float)
# df['Longitude'] = df['Longitude'].fillna(0).astype(float)
# df['Buffer'] = df['Buffer'].fillna(0).astype(float)
df = df[df['Buffer'].notna()].reset_index()
# df.dropna(subset=['Buffer'], inplace=True)
df
| index | Typhoon | Province | Project Name | Location | Latitude | Longitude | Buffer | Classification | Full Project Title | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | Yolanda | Cebu | Maya Park | Brgy. Maya, Daanbantayan, Cebu | 11.271009 | 124.048967 | 230.0 | Identified | Maya Park Homes | |||
| 1 | 2 | Yolanda | Cebu | Lanao Unity Park | Brgy. Lanao, Daanbantayan, Cebu | 11.248469 | 124.022224 | 280.0 | Identified | Lanao Unity Park Homes | |||
| 2 | 3 | Yolanda | Cebu | Daanbantayan 3 | Brgy. Tominjao, Daanbantayan, Cebu | 11.221464 | 124.036788 | 260.0 | Probable | Green Heights 1 (Daanbantayan 3) | |||
| 3 | 4 | Yolanda | Cebu | Daanbantayan 4 | Brgy. Tominjao, Daanbantayan, Cebu | 11.173075 | 124.015380 | 160.0 | Identified | Green Heights 2 (Daanbantayan 4) | |||
| 4 | 5 | Yolanda | Cebu | Daanbantayan Site 1 | Brgy. Tapilon, Daanbantayan Site 1, Cebu | 11.260164 | 124.036611 | 160.0 | Identified | Kandaya Homes 1 (Daanbantayan Site 1) | |||
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 225 | 286 | Reming | Albay | Polangui Resettlement Project | Lanigay, Polangui, Albay | 13.317859 | 123.480334 | 220.0 | Probable | Polangui Resettlement Project (Cielo Ville Ka... | |||
| 226 | 287 | Reming | Albay | Quitago Resettlement Project | Quitago, Guinobatan, Albay | 13.169667 | 123.607585 | 240.0 | Identified | Quitago Resettlement Project | |||
| 227 | 288 | Reming | Albay | Mauraro Resettlement Project | Mauraro, Guinobatan, Albay | 13.141681 | 123.598459 | 240.0 | Identified | Mauraro Resettlement Project | |||
| 228 | 289 | Reming | Albay | Bascaran Resettlement Project | Bascaran, Daraga, Albay | 13.097414 | 123.695317 | 110.0 | Probable | Bascaran Resettlement Project | |||
| 229 | 290 | Reming | Albay | Baligang Resettlement Project | Baligang, Camalig, Albay | 13.133385 | 123.637592 | 300.0 | Identified | Baligang Resettlement Project |
230 rows × 13 columns
import matplotlib.pyplot as plt
import seaborn as sns
# plt.figure(figsize = (15,3), dpi = 150)
g = sns.catplot(y = 'Typhoon', data = df, kind='count',
palette = 'gnuplot2', order = df['Typhoon'].value_counts().index)
plt.grid(alpha = 0.5)
ax = g.facet_axis(0, 0)
for c in ax.containers:
labels = [f'{(v.get_width()):1.0f}' for v in c]
ax.bar_label(c, labels=labels, label_type='edge')
plt.xlabel('Number of resettlement sites')
plt.show()
g = sns.catplot(y = 'Province', data = df, kind='count',
palette = 'viridis', order = df['Province'].value_counts().index)
plt.grid(alpha = 0.5)
ax = g.facet_axis(0, 0)
for c in ax.containers:
labels = [f'{(v.get_width()):1.0f}' for v in c]
ax.bar_label(c, labels=labels, label_type='edge')
plt.xlabel('Number of resettlement sites')
plt.show()
plot_size = 5
pal = list(sns.color_palette(palette='gnuplot2_r', n_colors=3).as_hex())
plt.figure(dpi = 150, facecolor = None)
plt.gcf().set_size_inches(plot_size, plot_size*1.5)
ax = plt.subplot(projection='polar')
# input_df.reset_index(inplace=True, drop=True)
numbers = [210, 334, 468]
list_details = ['Indentified + Probable sites', 'Unique sites', 'Original NHA List']
ax.set_theta_zero_location('N')
ax.set_theta_direction(1)
ax.set_rlabel_position(0)
ax.set_thetagrids([], labels=[])
ax.set_thetagrids(np.arange(0,360,36), labels=(np.arange(0,360,36)*50/36).astype(int), fontsize = 8)
for i in range(3):
ax.barh(i, numbers[i]*2*np.pi/500, label=list_details[i], color=pal[i])
plt.legend(loc = 0, fontsize=15, bbox_to_anchor=(1, -0.1))
ax.set_rgrids(range(3), labels = numbers, fontsize = 15, color = 'r')
ax = plt.subplot(projection='polar')
# plt.title(column_name + ": " + str(category))
# plt.tight_layout()
# plt.savefig(results + str(category) + ".png")
# plt.show()
reg_names = ['NCR',
'CAR',
'Region I',
'Region II',
'Region III',
'Region IV-A',
'Region IV-B',
'Region V',
'Region VI',
'Region VII',
'Region VIII',
'Region IX',
'Region X',
'Region XI',
'Region XII',
'Region XIII',
'ARMM']
reg_codes = [2355, 2354, 2356, 2357,
67165, 67166, 67167,
2361, 2362, 2363, 2364,
67159, 67160, 67161, 67162,
2368, 67156]
# viirs_ph = ee.ImageCollection("NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG").select('avg_rad')
# reg_geoms = ee.FeatureCollection("FAO/GAUL/2015/level1").filter(ee.Filter.inList('ADM1_CODE', reg_codes))
# # function to reduce our collection of geometries'
# def get_reg_avg_rad(img):
# return img.reduceRegions(reducer=ee.Reducer.mean(), collection=reg_geoms, scale=500)
# # function to get individual image dates
# def get_date(img):
# return img.set('date', img.date().format())
# # map these functions to our image collection
# reduced_regions = viirs_ph.map(get_reg_avg_rad).flatten()
# dates = viirs_ph.map(get_date)
# # get lists
# key_cols = ['ADM1_CODE','mean']
# regions_list = reduced_regions.reduceColumns(ee.Reducer.toList(len(key_cols)), key_cols).values()
# dates_list = dates.reduceColumns(ee.Reducer.toList(1), ['date']).values()
# # some numpy maneuvers to structure our data
# df_ph = pd.DataFrame(np.asarray(regions_list.getInfo()).squeeze(), columns=key_cols)
# dates = np.asarray(dates_list.getInfo()).squeeze()
# for regions in tqdm(reg_codes, 'Processing Regional Extraction:'):
# df_ph.loc[df_ph['ADM1_CODE']==regions,'dates'] = dates
# df_ph['dates'] = pd.to_datetime(df_ph['dates'])
# df_ph.set_index('dates', inplace=True)
# df_ph['mean'] = df_ph['mean'].astype(float)
# df_ph
NTL_dataset_initialize(Province, Region)¶def NTL_dataset_initialize(Province, Region):
viirs = ee.ImageCollection('NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG').filter(ee.Filter.date('2014-01-01', '2023-12-01'))
province = ee.Feature(ee.FeatureCollection("FAO/GAUL/2015/level2").filter(ee.Filter.eq('ADM2_NAME', Province)).first()).geometry()
region = ee.Feature(ee.FeatureCollection("FAO/GAUL/2015/level1").filter(ee.Filter.eq('ADM1_CODE', Region)).first()).geometry()
def get_prov_mean(img):
img = img.updateMask(img.gte(0))
mean = img.reduceRegion(reducer=ee.Reducer.mean(), geometry=province, scale=500, maxPixels=1e9).get('avg_rad')
return img.set('date', img.date().format()).set('mean',mean)
def get_reg_mean(img):
img = img.updateMask(img.gte(0))
mean = img.reduceRegion(reducer=ee.Reducer.mean(), geometry=region, scale=500, maxPixels=1e9).get('avg_rad')
return img.set('date', img.date().format()).set('mean',mean)
# reduce collection
prov_mean = viirs.map(get_prov_mean)
reg_mean = viirs.map(get_reg_mean)
# get lists
nested_list = prov_mean.reduceColumns(ee.Reducer.toList(2), ['date','mean']).values().get(0)
nested_list_2 = reg_mean.reduceColumns(ee.Reducer.toList(1), ['mean']).values().get(0)
regional_NTL = np.asarray(nested_list_2.getInfo()).squeeze()
# convert to dataframe
NTL_dataset = pd.DataFrame(nested_list.getInfo(), columns=['date', str(Province) + ' Province'])
NTL_dataset['date'] = pd.to_datetime(NTL_dataset['date'])
NTL_dataset = NTL_dataset.set_index('date')
NTL_dataset.insert(1, str(reg_names[reg_codes.index(Region)]), regional_NTL)
# plt.plot(NTL_dataset)
# plt.show()
return NTL_dataset
# NTL_dataset_initialize('Leyte',2364)
NTL_dataset = NTL_dataset_initialize('Leyte',2364)
NTL_dataset
# def NTL_dataset_initialize(Province, Region):
# viirs = ee.ImageCollection('NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG').filter(ee.Filter.date('2014-01-01', '2023-12-01'))
# province = ee.Feature(ee.FeatureCollection("FAO/GAUL/2015/level2").filter(ee.Filter.eq('ADM2_NAME', Province)).first()).geometry()
# region = ee.Feature(ee.FeatureCollection("FAO/GAUL/2015/level1").filter(ee.Filter.eq('ADM1_CODE', Region)).first()).geometry()
# def get_prov_mean(img):
# # img = img.updateMask(img.gte(0))
# mean = img.reduceRegion(reducer=ee.Reducer.mean(), geometry=province, scale=500, maxPixels=1e9).get('avg_rad')
# return img.set('date', img.date().format()).set('mean',mean)
# def get_reg_mean(img):
# # img = img.updateMask(img.gte(0))
# mean = img.reduceRegion(reducer=ee.Reducer.mean(), geometry=region, scale=500, maxPixels=1e9).get('avg_rad')
# return img.set('date', img.date().format()).set('mean',mean)
# # reduce collection
# prov_mean = viirs.map(get_prov_mean)
# reg_mean = viirs.map(get_reg_mean)
# # get lists
# nested_list = prov_mean.reduceColumns(ee.Reducer.toList(2), ['date','mean']).values().get(0)
# nested_list_2 = reg_mean.reduceColumns(ee.Reducer.toList(1), ['mean']).values().get(0)
# regional_NTL = np.asarray(nested_list_2.getInfo()).squeeze()
# # convert to dataframe
# NTL_dataset = pd.DataFrame(nested_list.getInfo(), columns=['date', str(Province) + ' Province'])
# NTL_dataset['date'] = pd.to_datetime(NTL_dataset['date'])
# NTL_dataset = NTL_dataset.set_index('date')
# NTL_dataset.insert(1, str(reg_names[reg_codes.index(Region)]), regional_NTL)
# plt.plot(NTL_dataset)
# plt.show()
# return NTL_dataset
| Leyte Province | Region VIII | |
|---|---|---|
| date | ||
| 2014-01-01 | 0.101630 | 0.087372 |
| 2014-02-01 | 0.170149 | 0.114812 |
| 2014-03-01 | 0.138900 | 0.098264 |
| 2014-04-01 | 0.217183 | 0.181112 |
| 2014-05-01 | 0.172025 | 0.111598 |
| ... | ... | ... |
| 2023-05-01 | 0.701959 | 0.529153 |
| 2023-06-01 | 0.618948 | 0.461296 |
| 2023-07-01 | 0.148886 | 0.147419 |
| 2023-08-01 | 0.458526 | 0.387465 |
| 2023-09-01 | 0.587854 | 0.433991 |
117 rows × 2 columns
print(NTL_dataset.index)
DatetimeIndex(['2014-01-01', '2014-02-01', '2014-03-01', '2014-04-01',
'2014-05-01', '2014-06-01', '2014-07-01', '2014-08-01',
'2014-09-01', '2014-10-01',
...
'2022-12-01', '2023-01-01', '2023-02-01', '2023-03-01',
'2023-04-01', '2023-05-01', '2023-06-01', '2023-07-01',
'2023-08-01', '2023-09-01'],
dtype='datetime64[ns]', name='date', length=117, freq=None)
STL_extraction(province, code, period)¶def STL_extraction(province, code, period):
region = str(reg_names[reg_codes.index(code)])
print("Processing NTL extraction for resettlements in " + str(province) + " Province, " + str(region))
NTL_dataset = NTL_dataset_initialize(province, code)
prov_df = df.loc[df['Province']==province,:]
prov_df = prov_df.reset_index(drop=True)
prov_df.index = prov_df.index + 1
for i in tqdm(range(1,len(prov_df)+1), desc = region + ": Processing"):
NTL_data = time_series_(prov_df['Latitude'][i], prov_df['Longitude'][i], prov_df['Buffer'][i])
NTL_data.index = pd.to_datetime(NTL_data.index)
idx = pd.date_range(start="2014-01-01",end="2023-09-01", freq='MS')
NTL_data = NTL_data.reindex(idx)
NTL_data = NTL_data.interpolate(limit_direction='both')
NTL_dataset.insert(i+1, prov_df['Project Name'][i], NTL_data)
trend = NTL_dataset.copy()
seasonality = NTL_dataset.copy()
residual = NTL_dataset.copy()
for loc in tqdm(NTL_dataset.columns, desc = 'STL Decomposition'):
data = NTL_dataset[loc].values
decomposition = sm.tsa.seasonal_decompose(data, model='additive', period = period)
trend[loc] = decomposition.trend
seasonality[loc] = decomposition.seasonal
residual[loc] = decomposition.resid
dataframe = pd.concat([NTL_dataset,trend, seasonality, residual], axis = 1, keys = ['Raw', 'Trend', 'Seasonality', 'Residual'])
return dataframe
# STL_extraction(province = 'Laguna', code = 67166, period = 12)
dataframe = STL_extraction(province = 'Leyte', code = 2364, period = 12)
dataframe
Processing NTL extraction for resettlements in Leyte Province, Region VIII
Region VIII: Processing: 100%|██████████| 38/38 [00:15<00:00, 2.46it/s] STL Decomposition: 100%|██████████| 40/40 [00:00<00:00, 766.40it/s]
| Raw | ... | Residual | |||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Leyte Province | Region VIII | Kawayan Ville | Villa Diana | Ridge View Park | New Hope Village | Knightsridge Residences | Villa Sofia | North Hill Arbours | Salvacion Heights | ... | Villa Soledad | Virlyn Heights Subdivision | Tabango Ville | Dulag Town Ville | Barugo Town Ville | Villa Verde | Capoocan Village | Dagami Town Ville | Sta. Fe Village Site 1 | Cool Spring Residences | |
| date | |||||||||||||||||||||
| 2014-01-01 | 0.101630 | 0.087372 | 0.262384 | 0.225237 | 0.240081 | 0.259939 | 0.250861 | 0.207366 | 0.278039 | 0.290849 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2014-02-01 | 0.170149 | 0.114812 | 0.111189 | 0.107876 | 0.143410 | 0.163447 | 0.108903 | 0.098870 | 0.126072 | 0.084186 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2014-03-01 | 0.138900 | 0.098264 | 0.059881 | 0.080907 | 0.007060 | 0.065777 | 0.054506 | 0.092130 | 0.044625 | 0.062283 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2014-04-01 | 0.217183 | 0.181112 | 0.090566 | 0.053938 | 0.099713 | 0.043851 | 0.078866 | 0.064774 | 0.029750 | 0.394358 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2014-05-01 | 0.172025 | 0.111598 | 0.121251 | 0.026969 | 0.047443 | 0.021926 | 0.066565 | 0.032387 | 0.014875 | 0.102708 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-05-01 | 0.701959 | 0.529153 | 0.648243 | 0.811948 | 0.591814 | 0.431255 | 0.643906 | 0.574933 | 0.732504 | 0.581307 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2023-06-01 | 0.618948 | 0.461296 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2023-07-01 | 0.148886 | 0.147419 | 1.413277 | 1.359351 | 1.327103 | 1.486052 | 1.391000 | 1.296726 | 1.438504 | 1.409091 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2023-08-01 | 0.458526 | 0.387465 | 0.387297 | 0.435325 | 0.426902 | 0.581734 | 0.462375 | 0.452197 | 0.566908 | 0.407273 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2023-09-01 | 0.587854 | 0.433991 | 0.370541 | 0.110000 | 0.237657 | 0.380923 | 0.214219 | 0.328789 | 0.330571 | 0.257216 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
117 rows × 160 columns
province = 'Leyte'
prov_df = df.loc[df['Province']==province,:]
prov_df = prov_df.reset_index(drop=True)
prov_df.index = prov_df.index + 1
prov_df
| index | Typhoon | Province | Project Name | Location | Latitude | Longitude | Buffer | Classification | Full Project Title | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 24 | Yolanda | Leyte | Kawayan Ville | Brgy, Kawayan, Tacloban City | 11.323226 | 124.954521 | 230.0 | Identified | Kawayan Ville I and II | |||
| 2 | 25 | Yolanda | Leyte | Villa Diana | Brgy. New Kawayan, Tacloban City, Leyte | 11.331256 | 124.953063 | 120.0 | Identified | Villa Diana | |||
| 3 | 26 | Yolanda | Leyte | Ridge View Park | Brgy. Cabalawan, Tacloban City, Leyte | 11.301019 | 124.952880 | 270.0 | Identified | Ridge View Park 1 | |||
| 4 | 27 | Yolanda | Leyte | New Hope Village | Brgy. New Kawayan & Brgy. Sta. Elena, Tacloban... | 11.341332 | 124.944084 | 220.0 | Identified | New Hope Village | |||
| 5 | 28 | Yolanda | Leyte | Knightsridge Residences | Brgy. Camansihay, Tacloban City, Leyte | 11.304363 | 124.940941 | 240.0 | Probable | Knightsridge Residences | |||
| 6 | 29 | Yolanda | Leyte | Villa Sofia | Brgy. Tagpuro, Tacloban City, Leyte | 11.343973 | 124.966135 | 200.0 | Identified | Villa Sofia | |||
| 7 | 30 | Yolanda | Leyte | North Hill Arbours | Brgy. Sto. Niño (Suhi), Tacloban City | 11.323519 | 124.947365 | 330.0 | Identified | North Hill Arbours | |||
| 8 | 31 | Yolanda | Leyte | Salvacion Heights | Brgy. Salvacion (Tigbao), Tacloban City, Leyte | 11.226967 | 124.952686 | 180.0 | Identified | Salvacion Heights | |||
| 9 | 32 | Yolanda | Leyte | Guadalupe Heights | Brgy. San Isidro (Suhi), Tacloban City, Leyte | 11.318076 | 124.944458 | 500.0 | Identified | Guadalupe Heights 1, 2 and 3 with Greendale Re... | |||
| 10 | 33 | Yolanda | Leyte | St. Francis Village | Brgy. Suhi, Tacloban City | 11.319448 | 124.956916 | 230.0 | Probable | St. Francis Village | |||
| 11 | 34 | Yolanda | Leyte | Pope Francis Village | Brgy. 99, Diit, Tacloban City | 11.260468 | 124.961335 | 240.0 | Identified | Pope Francis Village | |||
| 12 | 35 | Yolanda | Leyte | Kapuso Village Project Palo | Brgy. San Jose, Palo, Leyte | 11.173572 | 124.980725 | 100.0 | Identified | Kapuso Village Project Palo | |||
| 13 | 36 | Yolanda | Leyte | Ver Dominic Heights | Brgy. San Jose, Palo Site 1, Leyte | 11.175870 | 124.984272 | 190.0 | Probable | Ver Dominic Heights | |||
| 14 | 37 | Yolanda | Leyte | Prime Town Phase 1 | Brgy. Pago, Tanauan, Leyte | 11.101975 | 125.001334 | 120.0 | Identified | Prime Town Phase 1 | |||
| 15 | 38 | Yolanda | Leyte | Prime Town Phase 3 | Brgy. Sacme, Tanauan, Leyte | 11.070821 | 125.018836 | 210.0 | Identified | Prime Town Phase 3 | |||
| 16 | 39 | Yolanda | Leyte | Channel Ridge View | Brgy. Poblacion, Babatngon, Leyte | 11.419122 | 124.840478 | 350.0 | Identified | Channel Ridge View | |||
| 17 | 40 | Yolanda | Leyte | Pastrana Ville | Brgy. Aringit, Pastrana, Leyte | 11.117005 | 124.882018 | 300.0 | Identified | Pastrana Ville | |||
| 18 | 41 | Yolanda | Leyte | Carigara Housing Project | Brgy. Parag-um, Carigara, Leyte | 11.264629 | 124.726101 | 200.0 | Probable | Carigara Housing Project | |||
| 19 | 42 | Yolanda | Leyte | Carigara Housing Project 1 | Brgy. Sagkahan, Carigara, Leyte | 11.281128 | 124.728201 | 170.0 | Identified | Carigara Housing Project 1 | |||
| 20 | 43 | Yolanda | Leyte | Ecoville | Brgy. Sagkahan, Carigara, Leyte | 11.280059 | 124.721912 | 300.0 | Identified | Ecoville 1&2 | |||
| 21 | 44 | Yolanda | Leyte | Vergabriel's Heights Subdivision | Brgy. Parag-um, Carigara, Leyte | 11.264581 | 124.726034 | 200.0 | Identified | Vergabriel's Heights Subdivision | |||
| 22 | 45 | Yolanda | Leyte | Mayorga Towne Homes | Brgy. A. Bonifacio, Mayorga, Leyte | 10.900203 | 124.975367 | 120.0 | Probable | Mayorga Towne Homes | |||
| 23 | 46 | Yolanda | Leyte | Paglaum Ville | Brgy. San Isidro, McArthur, Leyte | 10.829067 | 124.992899 | 90.0 | Probable | Paglaum Ville | |||
| 24 | 47 | Yolanda | Leyte | Mont Eagle Ville IV | Brgy. Cangmoya Site 1, Palompon, Leyte | 10.988231 | 124.429969 | 280.0 | Identified | Mont Eagle Ville IV Subdivision Site 1 | |||
| 25 | 48 | Yolanda | Leyte | Tolosa Haiyan Village | Brgy. Tanghas, Tolosa, Leyte | 11.052954 | 125.031086 | 200.0 | Probable | Tolosa Haiyan Village & St. Michael Residences | |||
| 26 | 49 | Yolanda | Leyte | Matlang Heights | Matlang, Isabel, Leyte | 10.894580 | 124.456139 | 250.0 | Identified | Matlang Heights 1-2 | |||
| 27 | 50 | Yolanda | Leyte | Coconut Grove Subdivision | Brgy. Dita, Julita, Leyte | 10.972429 | 124.948448 | 130.0 | Probable | Coconut Grove Subdivision | |||
| 28 | 51 | Yolanda | Leyte | Bagong Pag-Asa Subdivision | Brgy. Margen Site 1 (and 2), Ormoc City, Leyte | 11.021036 | 124.530845 | 300.0 | Probable | Bagong Pag-Asa Subdivision Phase 1 and 2 | |||
| 29 | 52 | Yolanda | Leyte | Villa Soledad | Brgy. Punta and Jaena, Baybay City, Leyte | 10.635620 | 124.782669 | 200.0 | Identified | Villa Soledad | |||
| 30 | 53 | Yolanda | Leyte | Virlyn Heights Subdivision | Brgy. Sambel, Barauen, Leyte | 10.947098 | 124.907274 | 120.0 | Probable | Virlyn Heights Subdivision | |||
| 31 | 54 | Yolanda | Leyte | Tabango Ville | Brgy. Manlawaan, Tabango, Leyte | 11.333094 | 124.423100 | 100.0 | Probable | Tabango Ville | |||
| 32 | 55 | Yolanda | Leyte | Dulag Town Ville | Brgy. San Antonio, Dulag, Leyte | 10.950564 | 125.027621 | 110.0 | Probable | Dulag Town Ville | |||
| 33 | 56 | Yolanda | Leyte | Barugo Town Ville | Brgy. Busay, Barugo, Leyte | 11.324979 | 124.770885 | 190.0 | Probable | Barugo Town Ville Phase 1 & 2 | |||
| 34 | 57 | Yolanda | Leyte | Villa Verde | Brgy. Iligay, Villaba, Leyte | 11.253827 | 124.447524 | 180.0 | Probable | Villa Verde | |||
| 35 | 58 | Yolanda | Leyte | Capoocan Village | Brgy. Balucanad, Capoocan, Leyte | 11.281677 | 124.646016 | 200.0 | Identified | Capoocan Village | |||
| 36 | 59 | Yolanda | Leyte | Dagami Town Ville | Brgy. Sawahon, Dagami, Leyte | 11.070834 | 124.876392 | 120.0 | Identified | Dagami Town Ville | |||
| 37 | 60 | Yolanda | Leyte | Sta. Fe Village Site 1 | Brgy. Bulod, Sta. Fe, Leyte | 11.173139 | 124.924521 | 130.0 | Probable | Sta. Fe Village Site 1 | |||
| 38 | 61 | Yolanda | Leyte | Cool Spring Residences | Brgy. Arado, Burauen, Leyte | 10.979356 | 124.920860 | 120.0 | Probable | Cool Spring Residences (Burauen Housing Project) |
# code = 2364
# period = 12
# region = str(reg_names[reg_codes.index(code)])
# for i in tqdm(range(1,len(prov_df[:1])+1), desc = region + ": Processing"):
# NTL_data = time_series_(prov_df['Latitude'][i], prov_df['Longitude'][i], prov_df['Buffer'][i])
# NTL_data.index = pd.to_datetime(NTL_data.index)
# idx = pd.date_range(start="2014-01-01",end="2023-09-01", freq='MS')
# NTL_data = NTL_data.reindex(idx)
# NTL_data
# NTL_data.index = pd.to_datetime(NTL_data.index)
# print(pd.date_range(start="2014-01-01",end="2023-09-01", freq='MS').difference(NTL_data.index))
province = 'Leyte'
code = 2364
period = 12
region = str(reg_names[reg_codes.index(code)])
# fig, ax = plt.subplots(nrows=4,ncols=1, sharex='col',
# gridspec_kw={'height_ratios':[0.75,1,0.5,0.5]},
# figsize=(10,5), dpi = 200)
import matplotlib
matplotlib.rcParams.update(matplotlib.rcParamsDefault)
fig, ax = plt.subplots(nrows=4,ncols=1, sharex='col',
gridspec_kw={'height_ratios':[0.75,2,0.75,0.5]},
figsize=(14,6), dpi = 200)
# matplotlib.rcParams['axes.edgecolor'] = 'white'
# plt.style.context('dark_background')
# plt.style.use('dark_background')
# plt.rcParams.update({
# "lines.color": "white",
# "patch.edgecolor": "white",
# "text.color": "black",
# "axes.facecolor": "white",
# "axes.edgecolor": "lightgray",
# "axes.labelcolor": "white",
# "xtick.color": "white",
# "ytick.color": "white",
# "grid.color": "lightgray",
# "figure.facecolor": "black",
# "figure.edgecolor": "black",
# "savefig.facecolor": "black",
# "savefig.edgecolor": "black"})
# fig.patch.set_facecolor('white')
# fig.patch.set_alpha(0)
# ax.xaxis.label.set_color('white') #setting up X-axis label color to yellow
# ax.yaxis.label.set_color('white') #setting up Y-axis label color to blue
# ax.tick_params(axis='x', colors='white') #setting up X-axis tick color to red
# ax.tick_params(axis='y', colors='white') #setting up Y-axis tick color to black
date = dataframe.index
trend = dataframe['Trend']
seasonality = dataframe['Seasonality']
residual = dataframe['Residual']
prov_df = df.loc[df['Province']==province,:]
for i in tqdm(range(len(prov_df)), desc = 'Processing: '):
data = dataframe['Raw'].iloc[:,i+2].values
line_color = plt.cm.Spectral(i/len(prov_df))
ax[0].plot(date, data, color = line_color, linewidth = 1, alpha = 0.8)
ax[0].set_ylabel('$nW ·cm^{−2}·sr^{−1}$', color = 'white')
ax[1].plot(date, trend.iloc[:,i+2], color = line_color,
alpha = 0.8, label= dataframe['Raw'].columns[i+2])
ax[1].set_ylabel('trend')
ax[2].plot(date, seasonality.iloc[:,i+2], color = line_color,
linewidth = 1, alpha = 0.6)
ax[2].set_ylabel('seasonality')
resid = residual.iloc[:,i+2]
ax[3].bar(date, resid, width = 10, color = line_color, alpha = 0.8)
ax[3].set_ylabel('residual')
ax[1].legend(fontsize=8, ncol = 5, bbox_to_anchor= (0, 1.05, 1, 0.5), loc="lower left",
bbox_transform=ax[0].transAxes, fancybox = True, mode='expand')
ax[0].plot(dataframe['Raw'].iloc[:,0], 'b-', drawstyle = 'steps-mid', linewidth = 1, alpha = 0.5)
ax[0].plot(dataframe['Raw'].iloc[:,1], 'k-', drawstyle = 'steps-mid', linewidth = 1, alpha = 0.5)
ax[1].plot(trend.iloc[:,0], 'b.-', drawstyle = 'steps-mid', label= str(province) + ' Province', linewidth = 3, alpha = 0.8)
ax[1].plot(trend.iloc[:,1], 'k.-', drawstyle = 'steps-mid', label= str(region), linewidth = 2, alpha = 0.8)
ax[2].plot(seasonality.iloc[:,0], 'b-', drawstyle = 'steps-mid', linewidth = 1.5, alpha = 0.8)
ax[2].plot(seasonality.iloc[:,1], 'k-', drawstyle = 'steps-mid', linewidth = 1, alpha = 0.8)
ax[3].bar(date, residual.iloc[:,0], color = 'b', width = 15, alpha = 0.8)
ax[3].bar(date, residual.iloc[:,1], color = 'k', width = 12, alpha = 0.8)
for ax in ax:
ax.grid(alpha = 0.5)
ax.axvline(datetime.date(2020, 3, 10), color = 'r', lw = 5, alpha = 0.2) #For COVID lockdown
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1f')) # 2 decimal places)
ax.minorticks_on()
ax.xaxis.grid(True, which='minor', alpha = 0.2)
dtFmt = mdates.DateFormatter('%Y') # define the formatting
plt.gca().xaxis.set_major_formatter(dtFmt) # apply the format to the desired axis
plt.tight_layout()
# plt.savefig(results+"/CH6-TS-"+str(province)+".png", bbox_inches="tight")
plt.show()
Processing: 100%|██████████| 38/38 [00:06<00:00, 6.22it/s]
dataframe.index
DatetimeIndex(['2014-01-01', '2014-02-01', '2014-03-01', '2014-04-01',
'2014-05-01', '2014-06-01', '2014-07-01', '2014-08-01',
'2014-09-01', '2014-10-01',
...
'2022-12-01', '2023-01-01', '2023-02-01', '2023-03-01',
'2023-04-01', '2023-05-01', '2023-06-01', '2023-07-01',
'2023-08-01', '2023-09-01'],
dtype='datetime64[ns]', name='date', length=117, freq=None)
trend
| Leyte Province | Region VIII | Kawayan Ville | Villa Diana | Ridge View Park | New Hope Village | Knightsridge Residences | Villa Sofia | North Hill Arbours | Salvacion Heights | ... | Villa Soledad | Virlyn Heights Subdivision | Tabango Ville | Dulag Town Ville | Barugo Town Ville | Villa Verde | Capoocan Village | Dagami Town Ville | Sta. Fe Village Site 1 | Cool Spring Residences | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| date | |||||||||||||||||||||
| 2014-01-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2014-02-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2014-03-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2014-04-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2014-05-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-05-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2023-06-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2023-07-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2023-08-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2023-09-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
117 rows × 40 columns
# y = trend['Leyte Province'].dropna().values
y = trend.dropna().values
x = np.arange(len(y))
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
slope, intercept, r_value, p_value, std_err
fig, ax = plt.subplots(nrows=2,ncols=1,
sharex='col', gridspec_kw={'height_ratios':[0.75,0.25]},
figsize=(15,6), dpi = 200)
ax[0].grid(alpha = 0.3)
# ax.plot(dataframe['Raw'].iloc[:,0]-0.01, 'k.-', drawstyle = 'steps-mid', linewidth = 2)
ax[0].plot(dataframe['Raw'].iloc[:,0], 'k.-', drawstyle = 'steps-mid', linewidth = 1, label = 'NTL average radiance ($y_t$)', alpha = 0.5)
trend = dataframe['Trend'].iloc[:,0]
ax[0].plot(trend, 'b.-', alpha = 1, ms = 15, lw = 5, label = 'NTL Trend ($T_t$)')
ax[0].plot(dataframe.index[6:-6], intercept + slope*x, 'r--', label = 'Best Fit Line on NTL Trend (ROC)', lw = 10, alpha = 0.8)
# ax[0].plot([date[6], date[-7]], [trend[6], trend[-7]], 'k--', label = 'Rate of Change', lw = 3)
# ax[0].plot([date[6], date[-7]], [trend[6], trend[-7]], 'yo')
# ax[0].legend(fontsize=7, ncol = 5, bbox_to_anchor= (0, 1.0, 1, 0.5), loc="lower left",
# bbox_transform=ax[0].transAxes, fancybox = True, mode='expand')
ax[0].legend(title = 'Leyte Province',fancybox = True)
ax[0].annotate('net change: +'+ str(np.round(slope*107, 3)), xy = (date[84],0.04), xytext=(date[84], 0.04))
ax[0].annotate('slope: +'+ str(np.round(slope*12, 3)) + ' per year', xy = (date[84],0.0), xytext=(date[84], 0.0))
ax[0].annotate('intercept: '+ str(np.round(intercept, 3)), xy = (date[84],-0.04), xytext=(date[84], -0.04))
ax[0].annotate('r-value: ' + str(np.round(r_value, 5)), xy = (date[84],-0.08), xytext=(date[84], -0.08))
ax[0].set_ylabel('$nW ·cm^{−2}·sr^{−1}$')
ax[1].grid(alpha = 0.3)
ax[1].plot(dataframe['Seasonality'].iloc[:,0], color = 'g',
ls = '-', drawstyle = 'steps-mid', alpha = 0.75, lw = 5, label = 'NTL Seasonality ($S_t$)')
ax[1].bar(date, dataframe['Residual'].iloc[:,0], width = 20, color = 'orange',
alpha = 1, label = 'Residual ($E_t$)')
# ax[1].set_xticks([date[0], date[27], date[54], date[-28], date[-1]])
ax[1].legend(ncol = 2, fancybox = True)
ax[1].set_ylabel('$nW ·cm^{−2}·sr^{−1}$')
dtFmt = mdates.DateFormatter('%Y') # define the formatting
plt.gca().xaxis.set_major_formatter(dtFmt) # apply the format to the desired axis
plt.tight_layout()
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-73-b47c53996ada> in <cell line: 4>() 2 y = trend.dropna().values 3 x = np.arange(len(y)) ----> 4 slope, intercept, r_value, p_value, std_err = stats.linregress(x,y) 5 6 slope, intercept, r_value, p_value, std_err /usr/local/lib/python3.10/dist-packages/scipy/stats/_stats_mstats_common.py in linregress(x, y, alternative) 165 # ssxm = mean( (x-mean(x))^2 ) 166 # ssxym = mean( (x-mean(x)) * (y-mean(y)) ) --> 167 ssxm, ssxym, _, ssym = np.cov(x, y, bias=1).flat 168 169 # R-value /usr/local/lib/python3.10/dist-packages/numpy/core/overrides.py in cov(*args, **kwargs) /usr/local/lib/python3.10/dist-packages/numpy/lib/function_base.py in cov(m, y, rowvar, bias, ddof, fweights, aweights, dtype) 2637 if not rowvar and y.shape[0] != 1: 2638 y = y.T -> 2639 X = np.concatenate((X, y), axis=0) 2640 2641 if ddof is None: /usr/local/lib/python3.10/dist-packages/numpy/core/overrides.py in concatenate(*args, **kwargs) ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 93 and the array at index 1 has size 40
def timeseries_resettlements(province, code, period):
region = str(reg_names[reg_codes.index(code)])
print("Processing NTL extraction for resettlements in " + str(province) + " Province, " + str(region))
NTL_dataset = NTL_dataset_initialize(province, code)
prov_df = df.loc[df['Province']==province,:]
prov_df = prov_df.reset_index(drop=True)
prov_df.index = prov_df.index + 1
for i in tqdm(range(1,len(prov_df)+1), desc = region + ": Processing"):
# NTL_data = time_series_(prov_df['Latitude'][i], prov_df['Longitude'][i], prov_df['Buffer'][i])
# NTL_dataset.insert(i+1, prov_df['Project Name'][i], NTL_data)
NTL_data = time_series_(prov_df['Latitude'][i], prov_df['Longitude'][i], prov_df['Buffer'][i])
NTL_data.index = pd.to_datetime(NTL_data.index)
idx = pd.date_range(start="2014-01-01",end="2023-09-01", freq='MS')
NTL_data = NTL_data.reindex(idx)
NTL_data = NTL_data.interpolate(limit_direction='both')
NTL_dataset.insert(i+1, prov_df['Project Name'][i], NTL_data)
trend = NTL_dataset.copy()
seasonality = NTL_dataset.copy()
residual = NTL_dataset.copy()
for loc in tqdm(NTL_dataset.columns, desc = 'STL Decomposition'):
data = NTL_dataset[loc].values
decomposition = sm.tsa.seasonal_decompose(data, model='additive', period = period)
trend[loc] = decomposition.trend
seasonality[loc] = decomposition.seasonal
residual[loc] = decomposition.resid
print("Plotting timeseries NTL data")
fig, ax = plt.subplots(nrows=4,ncols=1, sharex='col',
gridspec_kw={'height_ratios':[0.75,2,0.75,0.5]},
figsize=(15,5), dpi = 200)
date = NTL_dataset.index
NTL_dataset.copy()
ax[0].plot(NTL_dataset.iloc[:,0], 'b.-', drawstyle = 'steps-mid', linewidth = 3, alpha = 0.5)
ax[0].plot(NTL_dataset.iloc[:,1], 'k.-', drawstyle = 'steps-mid', linewidth = 2, alpha = 0.5)
ax[1].plot(trend.iloc[:,0], 'b.-', drawstyle = 'steps-mid', label= str(province) + ' Province', linewidth = 3, alpha = 0.8)
ax[1].plot(trend.iloc[:,1], 'k.-', drawstyle = 'steps-mid', label= str(region), linewidth = 2, alpha = 0.8)
ax[2].plot(seasonality.iloc[:,0], 'b-', drawstyle = 'steps-mid', linewidth = 1.5, alpha = 0.8)
ax[2].plot(seasonality.iloc[:,1], 'k-', drawstyle = 'steps-mid', linewidth = 1, alpha = 0.8)
ax[3].bar(date, residual.iloc[:,0], color = 'b', width = 15, alpha = 0.8)
ax[3].bar(date, residual.iloc[:,1], color = 'k', width = 12, alpha = 0.8)
for i in tqdm(range(len(prov_df)), desc = 'Processing: '):
data = NTL_dataset.iloc[:,i+2].values
line_color = plt.cm.Spectral(i/len(prov_df))
ax[0].plot(date, data, color = line_color, linewidth = 1, alpha = 0.8)
ax[0].set_ylabel('$nW ·cm^{−2}·sr^{−1}$')
ax[1].plot(date, trend.iloc[:,i+2], color = line_color,
linewidth = prov_df['Buffer'][i+1]/100, alpha = 0.8, label= NTL_dataset.columns[i+2])
ax[1].set_ylabel('trend')
ax[2].plot(date, seasonality.iloc[:,i+2], color = line_color,
linewidth = 1, alpha = 0.6)
ax[2].set_ylabel('seasonality')
resid = residual.iloc[:,i+2]
ax[3].bar(date, resid, width = 10, color = line_color, alpha = 0.8)
ax[3].set_ylabel('residual')
ax[1].legend(fontsize=8, ncol = 5, bbox_to_anchor= (0, 1.05, 1, 0.5), loc="lower left",
bbox_transform=ax[0].transAxes, fancybox = True, mode='expand')
for ax in ax:
ax.grid(alpha = 0.5)
ax.axvline(datetime.date(2020, 3, 10), color = 'r', lw = 5, alpha = 0.2) #For COVID lockdown
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1f')) # 2 decimal places)
ax.minorticks_on()
ax.xaxis.grid(True, which='minor', alpha = 0.2)
dtFmt = mdates.DateFormatter('%b-%Y') # define the formatting
plt.gca().xaxis.set_major_formatter(dtFmt) # apply the format to the desired axis
# plt.tight_layout()
# plt.savefig(results+"/CH6-TS-"+str(province)+".png", bbox_inches="tight")
plt.show()
return NTL_dataset, trend, seasonality, residual
NTL_dataset, trend, seasonality, residual = timeseries_resettlements(province = 'Leyte', code = 2364, period = 12)
Processing NTL extraction for resettlements in Leyte Province, Region VIII
Region VIII: Processing: 100%|██████████| 38/38 [00:15<00:00, 2.53it/s] STL Decomposition: 100%|██████████| 40/40 [00:00<00:00, 468.62it/s]
Plotting timeseries NTL data
Processing: 100%|██████████| 38/38 [00:08<00:00, 4.68it/s]
dataset = dataframe['Trend'].dropna()
stats_summary = pd.DataFrame(index = dataset.columns,
columns = ['slope', 'intercept', 'r', 'p', 'std'])
plt.figure(dpi = 150, figsize = (10,5))
plt.grid(alpha = 0.5)
for i in tqdm(range(len(dataset.columns))):
loc = dataset.columns[i]
y = dataset[loc]
x = np.arange(len(y))
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
stats_summary.loc[loc] = np.asarray(stats.linregress(x,y))
line_color = plt.cm.jet(i/len(dataset.columns))
if i > 1:
# plt.plot(dataset.index, y, '.-', color = line_color, alpha = 0.1)
plt.plot(dataset.index, intercept + slope*x, color = line_color, label=str(loc), alpha = 0.5)
else:
plt.plot(dataset.index, y, '.', color = plt.cm.magma(i), alpha = 0.1)
plt.plot(dataset.index, intercept + slope*x, color = plt.cm.RdPu((i+1)/2), lw = 5, label=str(loc), linestyle = '--')
# if i%5 == 0:
# plt.plot(dataset.index, y, '.', color = line_color, alpha = 0.5)
# plt.plot(dataset.index, intercept + slope*x, color = line_color, label=str(loc))
plt.legend(fontsize=6, ncol = 5, bbox_to_anchor= (0, 1.05, 1, 0.5), loc="lower left",
bbox_transform=plt.gca().transAxes, fancybox = True, mode='expand')
# plt.legend()
plt.show()
100%|██████████| 40/40 [00:00<00:00, 614.07it/s]
results_path = '/content/gdrive/MyDrive/Research - PhD/Results/Resettlements/'
os.listdir(results_path)
['NTL STL', 'trend stats']
stats_summary(province, code, period)¶def stats_summary(province, code, period):
dataframe = STL_extraction(province, code, period)
dataset = dataframe['Trend'].dropna()
dataframe.to_csv(results_path + "NTL STL/" + str(province) + '_df.csv')
stats_summary = pd.DataFrame(index = dataset.columns,
columns = ['slope', 'intercept', 'r', 'p', 'std'])
# plt.figure(dpi = 150, figsize = (10,5))
# plt.grid(alpha = 0.5)
for i in tqdm(range(len(dataset.columns))):
loc = dataset.columns[i]
y = dataset[loc]
x = np.arange(len(y))
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
stats_summary.loc[loc] = np.asarray(stats.linregress(x,y))
# line_color = plt.cm.jet(i/len(dataset.columns))
# if i > 1:
# # plt.plot(dataset.index, y, '.-', color = line_color, alpha = 0.1)
# plt.plot(dataset.index, intercept + slope*x, color = line_color, label=str(loc), alpha = 0.5)
# else:
# plt.plot(dataset.index, y, '.', color = plt.cm.RdPu((i+1)/2), alpha = 0.1)
# plt.plot(dataset.index, intercept + slope*x, color = plt.cm.RdPu((i+1)/2), lw = 5, label=str(loc), linestyle = '--')
# # if i%5 == 0:
# # plt.plot(dataset.index, y, '.', color = line_color, alpha = 0.5)
# # plt.plot(dataset.index, intercept + slope*x, color = line_color, label=str(loc))
# plt.legend(fontsize=6, ncol = 5, bbox_to_anchor= (0, 1.05, 1, 0.5), loc="lower left",
# bbox_transform=plt.gca().transAxes, fancybox = True, mode='expand')
# # plt.legend()
# plt.show()
# stats_summary.to_csv(results_path + "trend stats/" + str(province) + '_stats.csv')
return dataframe, stats_summary
Leyte_df, Leyte_stats = stats_summary(province = 'Leyte', code = 2364, period = 12)
Processing NTL extraction for resettlements in Leyte Province, Region VIII
Region VIII: Processing: 100%|██████████| 38/38 [00:16<00:00, 2.28it/s] STL Decomposition: 100%|██████████| 40/40 [00:00<00:00, 539.06it/s] 100%|██████████| 40/40 [00:00<00:00, 1229.79it/s]
Cebu_df, Cebu_stats = stats_summary(province = 'Cebu', code = 2363, period = 12)
Processing NTL extraction for resettlements in Cebu Province, Region VII
Region VII: Processing: 100%|██████████| 23/23 [00:18<00:00, 1.23it/s] STL Decomposition: 100%|██████████| 25/25 [00:00<00:00, 677.76it/s] 100%|██████████| 25/25 [00:00<00:00, 1960.54it/s]
Samar_df, Samar_stats = stats_summary(province = 'Samar', code = 2364, period = 12)
Processing NTL extraction for resettlements in Samar Province, Region VIII
Region VIII: Processing: 100%|██████████| 19/19 [00:15<00:00, 1.19it/s] STL Decomposition: 100%|██████████| 21/21 [00:00<00:00, 710.92it/s] 100%|██████████| 21/21 [00:00<00:00, 1574.64it/s]
Biliran_df, Biliran_stats = stats_summary(province = 'Biliran', code = 2364, period = 12)
Processing NTL extraction for resettlements in Biliran Province, Region VIII
Region VIII: Processing: 100%|██████████| 12/12 [00:10<00:00, 1.18it/s] STL Decomposition: 100%|██████████| 14/14 [00:00<00:00, 155.39it/s] 100%|██████████| 14/14 [00:00<00:00, 375.20it/s]
Aklan_df, Aklan_stats = stats_summary(province = 'Aklan', code = 2362, period = 12)
Processing NTL extraction for resettlements in Aklan Province, Region VI
Region VI: Processing: 100%|██████████| 12/12 [00:13<00:00, 1.09s/it] STL Decomposition: 100%|██████████| 14/14 [00:00<00:00, 651.78it/s] 100%|██████████| 14/14 [00:00<00:00, 1079.95it/s]
Antique_df, Antique_stats = stats_summary(province = 'Antique', code = 2362, period = 12)
Processing NTL extraction for resettlements in Antique Province, Region VI
Region VI: Processing: 100%|██████████| 26/26 [00:22<00:00, 1.17it/s] STL Decomposition: 100%|██████████| 28/28 [00:00<00:00, 396.33it/s] 100%|██████████| 28/28 [00:00<00:00, 1143.95it/s]
MisOr_df, MisOr_stats = stats_summary(province = 'Misamis Oriental', code = 67160, period = 12)
Processing NTL extraction for resettlements in Misamis Oriental Province, Region X
Region X: Processing: 100%|██████████| 16/16 [00:12<00:00, 1.25it/s] STL Decomposition: 100%|██████████| 18/18 [00:00<00:00, 677.20it/s] 100%|██████████| 18/18 [00:00<00:00, 1041.65it/s]
Lanao_df, Lanao_stats = stats_summary(province = 'Lanao Del Norte', code = 67160, period = 12)
Processing NTL extraction for resettlements in Lanao Del Norte Province, Region X
Region X: Processing: 100%|██████████| 7/7 [00:05<00:00, 1.22it/s] STL Decomposition: 100%|██████████| 9/9 [00:00<00:00, 137.68it/s] 100%|██████████| 9/9 [00:00<00:00, 785.04it/s]
Albay_df, Albay_stats = stats_summary(province = 'Albay', code = 2361, period = 12)
Processing NTL extraction for resettlements in Albay Province, Region V
Region V: Processing: 100%|██████████| 14/14 [00:11<00:00, 1.17it/s] STL Decomposition: 100%|██████████| 16/16 [00:00<00:00, 599.14it/s] 100%|██████████| 16/16 [00:00<00:00, 1116.43it/s]
Catanduanes_df, Catanduanes_stats = stats_summary(province = 'Catanduanes', code = 2361, period = 12)
Processing NTL extraction for resettlements in Catanduanes Province, Region V
Region V: Processing: 100%|██████████| 2/2 [00:01<00:00, 1.94it/s] STL Decomposition: 100%|██████████| 4/4 [00:00<00:00, 376.16it/s] 100%|██████████| 4/4 [00:00<00:00, 794.71it/s]
Laguna_df, Laguna_stats = stats_summary(province = 'Laguna', code = 67166, period = 12)
Processing NTL extraction for resettlements in Laguna Province, Region IV-A
Region IV-A: Processing: 100%|██████████| 4/4 [00:03<00:00, 1.33it/s] STL Decomposition: 100%|██████████| 6/6 [00:00<00:00, 581.09it/s] 100%|██████████| 6/6 [00:00<00:00, 1019.11it/s]
Davao_df, Davao_stats = stats_summary(province = 'Davao Oriental', code = 67161, period = 12)
Processing NTL extraction for resettlements in Davao Oriental Province, Region XI
Region XI: Processing: 100%|██████████| 8/8 [00:07<00:00, 1.11it/s] STL Decomposition: 100%|██████████| 10/10 [00:00<00:00, 323.13it/s] 100%|██████████| 10/10 [00:00<00:00, 822.86it/s]
Bulacan_df, Bulacan_stats = stats_summary(province = 'Bulacan', code = 67165, period = 12)
Processing NTL extraction for resettlements in Bulacan Province, Region III
Region III: Processing: 100%|██████████| 4/4 [00:03<00:00, 1.19it/s] STL Decomposition: 100%|██████████| 6/6 [00:00<00:00, 563.38it/s] 100%|██████████| 6/6 [00:00<00:00, 958.22it/s]
Iloilo_df, Iloilo_stats = stats_summary(province = 'Iloilo', code = 2362, period = 12)
Processing NTL extraction for resettlements in Iloilo Province, Region VI
Region VI: Processing: 100%|██████████| 20/20 [00:18<00:00, 1.07it/s] STL Decomposition: 100%|██████████| 22/22 [00:00<00:00, 680.19it/s] 100%|██████████| 22/22 [00:00<00:00, 1671.95it/s]
Negros_Occidental_df, Negros_Occidental_stats = stats_summary(province = 'Negros Occidental', code = 2362, period = 12)
Processing NTL extraction for resettlements in Negros Occidental Province, Region VI
Region VI: Processing: 0it [00:00, ?it/s] STL Decomposition: 100%|██████████| 2/2 [00:00<00:00, 385.29it/s] 100%|██████████| 2/2 [00:00<00:00, 735.07it/s]
Rizal_df, Rizal_stats = stats_summary(province = 'Rizal', code = 67166, period = 12)
Processing NTL extraction for resettlements in Rizal Province, Region IV-A
Region IV-A: Processing: 100%|██████████| 2/2 [00:00<00:00, 2.28it/s] STL Decomposition: 100%|██████████| 4/4 [00:00<00:00, 414.14it/s] 100%|██████████| 4/4 [00:00<00:00, 786.33it/s]
Negros_Or_df, Negros_Or_stats = stats_summary(province = 'Negros Oriental', code = 2363, period = 12)
Processing NTL extraction for resettlements in Negros Oriental Province, Region VII
Region VII: Processing: 100%|██████████| 1/1 [00:00<00:00, 2.02it/s] STL Decomposition: 100%|██████████| 3/3 [00:00<00:00, 488.87it/s] 100%|██████████| 3/3 [00:00<00:00, 552.08it/s]
# NCR_df, NCR_stats = stats_summary(province = 'National Capital Region', code = 2355, period = 12)
# Ilocos_Norte_df, Ilocos_Norte_stats = stats_summary(province = 'Ilocos Norte', code = 2356, period = 12)
Processing NTL extraction for resettlements in Ilocos Norte Province, Region I
Region I: Processing: 0it [00:00, ?it/s] STL Decomposition: 100%|██████████| 2/2 [00:00<00:00, 508.99it/s] 100%|██████████| 2/2 [00:00<00:00, 647.22it/s]
Benguet_df, Bengue_stats = stats_summary(province = 'Benguet', code = 2354, period = 12)
Processing NTL extraction for resettlements in Benguet Province, CAR
CAR: Processing: 100%|██████████| 1/1 [00:00<00:00, 2.06it/s] STL Decomposition: 100%|██████████| 3/3 [00:00<00:00, 351.31it/s] 100%|██████████| 3/3 [00:00<00:00, 789.69it/s]
resettlement_stats = pd.concat([Leyte_stats.reset_index(drop=True),
Samar_stats.reset_index(drop=True),
Biliran_stats.reset_index(drop=True),
Cebu_stats.reset_index(drop=True),
Aklan_stats.reset_index(drop=True),
Antique_stats.reset_index(drop=True),
MisOr_stats.reset_index(drop=True),
Lanao_stats.reset_index(drop=True),
Albay_stats.reset_index(drop=True),
Catanduanes_stats.reset_index(drop=True),
Laguna_stats.reset_index(drop=True),
Davao_stats.reset_index(drop=True),
Bulacan_stats.reset_index(drop=True),
Rizal_stats.reset_index(drop=True)],
axis = 1,
keys = ['Leyte', 'Samar', 'Biliran', 'Cebu', 'Aklan', 'Antique', 'Mis Or', 'Lanao DN', 'Albay',
'Catanduanes', 'Laguna', 'Davao', 'Bulacan', 'Rizal'],
)
resettlement_stats
| Leyte | Samar | ... | Bulacan | Rizal | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| slope | intercept | r | p | std | slope | intercept | r | p | std | ... | slope | intercept | r | p | std | slope | intercept | r | p | std | |
| 0 | 0.003486 | 0.16042 | 0.900777 | 0.0 | 0.000173 | 0.003265 | 0.130459 | 0.916345 | 0.0 | 0.000147 | ... | 0.008516 | 0.748784 | 0.949969 | 0.0 | 0.000289 | 0.007295 | 1.130044 | 0.97378 | 0.0 | 0.000176 |
| 1 | 0.003232 | 0.060749 | 0.899108 | 0.0 | 0.000162 | 0.003232 | 0.060749 | 0.899108 | 0.0 | 0.000162 | ... | 0.004685 | 0.437237 | 0.953814 | 0.0 | 0.000152 | 0.005511 | 0.729565 | 0.97068 | 0.0 | 0.000141 |
| 2 | 0.00292 | 0.166143 | 0.742705 | 0.0 | 0.000272 | 0.003852 | 0.111861 | 0.675657 | 0.0 | 0.000433 | ... | 0.003333 | 0.163761 | 0.860448 | 0.0 | 0.000204 | 0.003107 | 0.156065 | 0.816606 | 0.0 | 0.000226 |
| 3 | 0.002866 | 0.175993 | 0.708536 | 0.0 | 0.000294 | 0.003362 | 0.142182 | 0.66659 | 0.0 | 0.000388 | ... | 0.002821 | 0.158615 | 0.846683 | 0.0 | 0.000183 | 0.003723 | 0.125531 | 0.886508 | 0.0 | 0.0002 |
| 4 | 0.002718 | 0.191979 | 0.675709 | 0.0 | 0.000306 | 0.003145 | 0.12185 | 0.705686 | 0.0 | 0.000326 | ... | 0.004108 | 0.141188 | 0.892473 | 0.0 | 0.000214 | NaN | NaN | NaN | NaN | NaN |
| 5 | 0.002907 | 0.206392 | 0.626332 | 0.0 | 0.000373 | 0.00156 | 0.220641 | 0.404609 | 0.000043 | 0.000364 | ... | 0.003766 | 0.131282 | 0.880035 | 0.0 | 0.00021 | NaN | NaN | NaN | NaN | NaN |
| 6 | 0.002496 | 0.188876 | 0.653172 | 0.0 | 0.000298 | 0.002409 | 0.166632 | 0.562825 | 0.0 | 0.000365 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 7 | 0.002933 | 0.167215 | 0.713369 | 0.0 | 0.000297 | 0.003371 | 0.120913 | 0.652759 | 0.0 | 0.000403 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 8 | 0.003314 | 0.155804 | 0.77092 | 0.0 | 0.000282 | 0.003774 | 0.137719 | 0.769347 | 0.0 | 0.000323 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 9 | 0.002622 | 0.185688 | 0.607865 | 0.0 | 0.000353 | 0.001863 | 0.188459 | 0.576986 | 0.0 | 0.000272 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 10 | 0.003302 | 0.154291 | 0.766497 | 0.0 | 0.000285 | 0.002125 | 0.171147 | 0.542177 | 0.0 | 0.00034 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 11 | 0.003085 | 0.164648 | 0.720109 | 0.0 | 0.000307 | 0.002659 | 0.177516 | 0.54897 | 0.0 | 0.000418 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 12 | 0.002618 | 0.209615 | 0.632839 | 0.0 | 0.00033 | 0.003413 | 0.142952 | 0.62642 | 0.0 | 0.000438 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 13 | 0.003552 | 0.12851 | 0.785184 | 0.0 | 0.000289 | 0.001957 | 0.2037 | 0.586661 | 0.0 | 0.000279 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 14 | 0.003505 | 0.12317 | 0.745053 | 0.0 | 0.000324 | 0.002105 | 0.161043 | 0.596407 | 0.0 | 0.000292 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 15 | 0.003173 | 0.14742 | 0.688385 | 0.0 | 0.000345 | 0.003272 | 0.158281 | 0.793357 | 0.0 | 0.000259 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 16 | 0.002258 | 0.161573 | 0.529293 | 0.0 | 0.000373 | 0.003849 | 0.140374 | 0.826192 | 0.0 | 0.000271 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 17 | 0.00332 | 0.109239 | 0.754241 | 0.0 | 0.000298 | 0.002638 | 0.141529 | 0.623157 | 0.0 | 0.000341 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 18 | 0.002711 | 0.145419 | 0.625876 | 0.0 | 0.000348 | 0.00369 | 0.127022 | 0.808635 | 0.0 | 0.000277 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 19 | 0.003714 | 0.096666 | 0.814768 | 0.0 | 0.000273 | 0.002661 | 0.124977 | 0.651076 | 0.0 | 0.00032 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 20 | 0.003409 | 0.116767 | 0.757866 | 0.0 | 0.000303 | 0.003631 | 0.15701 | 0.734336 | 0.0 | 0.000346 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 21 | 0.003332 | 0.105987 | 0.748031 | 0.0 | 0.000305 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 22 | 0.00371 | 0.096883 | 0.814834 | 0.0 | 0.000272 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 23 | 0.00174 | 0.140773 | 0.546222 | 0.0 | 0.000275 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 24 | 0.002587 | 0.109174 | 0.63308 | 0.0 | 0.000326 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 25 | 0.003623 | 0.126982 | 0.650032 | 0.0 | 0.000437 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 26 | 0.000745 | 0.206945 | 0.252603 | 0.013029 | 0.000295 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 27 | 0.003086 | 0.131114 | 0.605479 | 0.0 | 0.000418 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 28 | 0.000719 | 0.213052 | 0.267428 | 0.008436 | 0.000267 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 29 | 0.002225 | 0.165676 | 0.453346 | 0.000004 | 0.000451 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 30 | 0.001525 | 0.148101 | 0.486382 | 0.000001 | 0.000283 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 31 | 0.001595 | 0.203518 | 0.42671 | 0.000015 | 0.000349 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 32 | 0.002126 | 0.176337 | 0.704722 | 0.0 | 0.000221 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 33 | 0.001402 | 0.171176 | 0.371599 | 0.000193 | 0.000361 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 34 | 0.00281 | 0.146148 | 0.659787 | 0.0 | 0.00033 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 35 | 0.000838 | 0.224774 | 0.312282 | 0.001952 | 0.000263 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 36 | 0.004076 | 0.113552 | 0.816263 | 0.0 | 0.000298 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 37 | 0.002754 | 0.143618 | 0.690735 | 0.0 | 0.000297 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 38 | 0.003623 | 0.134008 | 0.723071 | 0.0 | 0.000357 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 39 | 0.001385 | 0.20143 | 0.374677 | 0.000169 | 0.000354 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
40 rows × 70 columns
# resettlement_stats.iloc[:, resettlement_stats.columns.get_level_values(1)=='slope']
# data = resettlement_stats.xs('slope', level=1, axis=1)
# len(data.columns)
data = resettlement_stats.xs('slope', level=1, axis=1)
data = data*108/12
# data.dropna(ignore_index=T).reset_index(drop=True)#.reset_index(drop=True, inplace=True)
data
# data.median().iloc[::-1].index
| Leyte | Samar | Biliran | Cebu | Aklan | Antique | Mis Or | Lanao DN | Albay | Catanduanes | Laguna | Davao | Bulacan | Rizal | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0.031378 | 0.029387 | 0.03179 | 0.032625 | 0.029412 | 0.03183 | 0.025002 | 0.025861 | 0.039682 | 0.029325 | 0.052604 | 0.027019 | 0.076641 | 0.065657 |
| 1 | 0.02909 | 0.02909 | 0.02909 | 0.02886 | 0.03315 | 0.03315 | 0.026123 | 0.026123 | 0.034828 | 0.034828 | 0.049601 | 0.02902 | 0.042161 | 0.049601 |
| 2 | 0.026278 | 0.034666 | 0.036738 | 0.015851 | 0.017977 | 0.024315 | 0.027235 | 0.017581 | 0.005628 | 0.012136 | 0.025165 | -0.003049 | 0.029993 | 0.027959 |
| 3 | 0.025796 | 0.030258 | 0.030803 | 0.005932 | 0.017932 | 0.023812 | 0.025313 | 0.001206 | 0.009447 | 0.018878 | 0.029781 | 0.004781 | 0.025392 | 0.03351 |
| 4 | 0.024459 | 0.028304 | -0.001608 | 0.006132 | 0.019519 | 0.018599 | 0.008441 | 0.008943 | 0.000089 | NaN | 0.027122 | -0.001174 | 0.036974 | NaN |
| 5 | 0.02616 | 0.014044 | 0.01197 | 0.012208 | 0.022133 | 0.01859 | 0.018563 | 0.008729 | 0.001148 | NaN | 0.033046 | 0.013245 | 0.033894 | NaN |
| 6 | 0.022464 | 0.021682 | 0.010145 | 0.011822 | 0.017162 | 0.020059 | 0.015015 | 0.01546 | 0.0124 | NaN | NaN | 0.006513 | NaN | NaN |
| 7 | 0.026398 | 0.030337 | 0.007239 | 0.019808 | 0.016086 | 0.017198 | 0.016325 | 0.022071 | 0.005318 | NaN | NaN | 0.018594 | NaN | NaN |
| 8 | 0.029822 | 0.033963 | 0.000346 | 0.016582 | 0.021421 | 0.018446 | 0.017828 | 0.017197 | 0.01018 | NaN | NaN | 0.010816 | NaN | NaN |
| 9 | 0.023595 | 0.016767 | -0.004085 | 0.005904 | 0.022399 | 0.020398 | 0.013913 | NaN | 0.009038 | NaN | NaN | 0.001939 | NaN | NaN |
| 10 | 0.029719 | 0.019125 | 0.001254 | 0.015721 | 0.012305 | 0.020482 | 0.012057 | NaN | 0.015801 | NaN | NaN | NaN | NaN | NaN |
| 11 | 0.027764 | 0.02393 | 0.025547 | 0.017321 | 0.018097 | 0.021205 | 0.011675 | NaN | 0.015811 | NaN | NaN | NaN | NaN | NaN |
| 12 | 0.023562 | 0.030717 | 0.030479 | 0.02051 | 0.013824 | 0.019993 | 0.008433 | NaN | 0.014675 | NaN | NaN | NaN | NaN | NaN |
| 13 | 0.03197 | 0.017611 | 0.037162 | 0.017887 | 0.011644 | 0.019548 | 0.021147 | NaN | 0.016476 | NaN | NaN | NaN | NaN | NaN |
| 14 | 0.031544 | 0.018949 | NaN | 0.020401 | NaN | 0.019081 | 0.008602 | NaN | 0.009898 | NaN | NaN | NaN | NaN | NaN |
| 15 | 0.028555 | 0.029451 | NaN | 0.027158 | NaN | 0.021428 | 0.013569 | NaN | 0.005618 | NaN | NaN | NaN | NaN | NaN |
| 16 | 0.020326 | 0.034641 | NaN | 0.027301 | NaN | 0.020808 | 0.007683 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 17 | 0.029877 | 0.023739 | NaN | 0.027221 | NaN | 0.023345 | 0.004854 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 18 | 0.024402 | 0.033211 | NaN | 0.005388 | NaN | 0.021428 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 19 | 0.03343 | 0.023949 | NaN | 0.017102 | NaN | 0.018047 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 20 | 0.030681 | 0.032677 | NaN | 0.011592 | NaN | 0.017839 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 21 | 0.029985 | NaN | NaN | 0.005061 | NaN | 0.00949 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 22 | 0.033389 | NaN | NaN | 0.027963 | NaN | 0.017688 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 23 | 0.015656 | NaN | NaN | 0.030017 | NaN | 0.015717 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 24 | 0.023279 | NaN | NaN | 0.009638 | NaN | 0.01858 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 25 | 0.032611 | NaN | NaN | NaN | NaN | 0.018477 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 26 | 0.006709 | NaN | NaN | NaN | NaN | 0.022115 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 27 | 0.027772 | NaN | NaN | NaN | NaN | 0.017787 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 28 | 0.006472 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 29 | 0.020024 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 30 | 0.013723 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 31 | 0.014355 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 32 | 0.019131 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 33 | 0.012616 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 34 | 0.025286 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 35 | 0.007545 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 36 | 0.036684 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 37 | 0.024785 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 38 | 0.032609 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 39 | 0.012466 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
index_sort = data[2:].mean().sort_values(ascending = False).index
data_sorted = data[index_sort]
data_sorted
| Bulacan | Rizal | Laguna | Samar | Leyte | Antique | Aklan | Cebu | Catanduanes | Biliran | Mis Or | Lanao DN | Albay | Davao | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0.076641 | 0.065657 | 0.052604 | 0.029387 | 0.031378 | 0.03183 | 0.029412 | 0.032625 | 0.029325 | 0.03179 | 0.025002 | 0.025861 | 0.039682 | 0.027019 |
| 1 | 0.042161 | 0.049601 | 0.049601 | 0.02909 | 0.02909 | 0.03315 | 0.03315 | 0.02886 | 0.034828 | 0.02909 | 0.026123 | 0.026123 | 0.034828 | 0.02902 |
| 2 | 0.029993 | 0.027959 | 0.025165 | 0.034666 | 0.026278 | 0.024315 | 0.017977 | 0.015851 | 0.012136 | 0.036738 | 0.027235 | 0.017581 | 0.005628 | -0.003049 |
| 3 | 0.025392 | 0.03351 | 0.029781 | 0.030258 | 0.025796 | 0.023812 | 0.017932 | 0.005932 | 0.018878 | 0.030803 | 0.025313 | 0.001206 | 0.009447 | 0.004781 |
| 4 | 0.036974 | NaN | 0.027122 | 0.028304 | 0.024459 | 0.018599 | 0.019519 | 0.006132 | NaN | -0.001608 | 0.008441 | 0.008943 | 0.000089 | -0.001174 |
| 5 | 0.033894 | NaN | 0.033046 | 0.014044 | 0.02616 | 0.01859 | 0.022133 | 0.012208 | NaN | 0.01197 | 0.018563 | 0.008729 | 0.001148 | 0.013245 |
| 6 | NaN | NaN | NaN | 0.021682 | 0.022464 | 0.020059 | 0.017162 | 0.011822 | NaN | 0.010145 | 0.015015 | 0.01546 | 0.0124 | 0.006513 |
| 7 | NaN | NaN | NaN | 0.030337 | 0.026398 | 0.017198 | 0.016086 | 0.019808 | NaN | 0.007239 | 0.016325 | 0.022071 | 0.005318 | 0.018594 |
| 8 | NaN | NaN | NaN | 0.033963 | 0.029822 | 0.018446 | 0.021421 | 0.016582 | NaN | 0.000346 | 0.017828 | 0.017197 | 0.01018 | 0.010816 |
| 9 | NaN | NaN | NaN | 0.016767 | 0.023595 | 0.020398 | 0.022399 | 0.005904 | NaN | -0.004085 | 0.013913 | NaN | 0.009038 | 0.001939 |
| 10 | NaN | NaN | NaN | 0.019125 | 0.029719 | 0.020482 | 0.012305 | 0.015721 | NaN | 0.001254 | 0.012057 | NaN | 0.015801 | NaN |
| 11 | NaN | NaN | NaN | 0.02393 | 0.027764 | 0.021205 | 0.018097 | 0.017321 | NaN | 0.025547 | 0.011675 | NaN | 0.015811 | NaN |
| 12 | NaN | NaN | NaN | 0.030717 | 0.023562 | 0.019993 | 0.013824 | 0.02051 | NaN | 0.030479 | 0.008433 | NaN | 0.014675 | NaN |
| 13 | NaN | NaN | NaN | 0.017611 | 0.03197 | 0.019548 | 0.011644 | 0.017887 | NaN | 0.037162 | 0.021147 | NaN | 0.016476 | NaN |
| 14 | NaN | NaN | NaN | 0.018949 | 0.031544 | 0.019081 | NaN | 0.020401 | NaN | NaN | 0.008602 | NaN | 0.009898 | NaN |
| 15 | NaN | NaN | NaN | 0.029451 | 0.028555 | 0.021428 | NaN | 0.027158 | NaN | NaN | 0.013569 | NaN | 0.005618 | NaN |
| 16 | NaN | NaN | NaN | 0.034641 | 0.020326 | 0.020808 | NaN | 0.027301 | NaN | NaN | 0.007683 | NaN | NaN | NaN |
| 17 | NaN | NaN | NaN | 0.023739 | 0.029877 | 0.023345 | NaN | 0.027221 | NaN | NaN | 0.004854 | NaN | NaN | NaN |
| 18 | NaN | NaN | NaN | 0.033211 | 0.024402 | 0.021428 | NaN | 0.005388 | NaN | NaN | NaN | NaN | NaN | NaN |
| 19 | NaN | NaN | NaN | 0.023949 | 0.03343 | 0.018047 | NaN | 0.017102 | NaN | NaN | NaN | NaN | NaN | NaN |
| 20 | NaN | NaN | NaN | 0.032677 | 0.030681 | 0.017839 | NaN | 0.011592 | NaN | NaN | NaN | NaN | NaN | NaN |
| 21 | NaN | NaN | NaN | NaN | 0.029985 | 0.00949 | NaN | 0.005061 | NaN | NaN | NaN | NaN | NaN | NaN |
| 22 | NaN | NaN | NaN | NaN | 0.033389 | 0.017688 | NaN | 0.027963 | NaN | NaN | NaN | NaN | NaN | NaN |
| 23 | NaN | NaN | NaN | NaN | 0.015656 | 0.015717 | NaN | 0.030017 | NaN | NaN | NaN | NaN | NaN | NaN |
| 24 | NaN | NaN | NaN | NaN | 0.023279 | 0.01858 | NaN | 0.009638 | NaN | NaN | NaN | NaN | NaN | NaN |
| 25 | NaN | NaN | NaN | NaN | 0.032611 | 0.018477 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 26 | NaN | NaN | NaN | NaN | 0.006709 | 0.022115 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 27 | NaN | NaN | NaN | NaN | 0.027772 | 0.017787 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 28 | NaN | NaN | NaN | NaN | 0.006472 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 29 | NaN | NaN | NaN | NaN | 0.020024 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 30 | NaN | NaN | NaN | NaN | 0.013723 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 31 | NaN | NaN | NaN | NaN | 0.014355 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 32 | NaN | NaN | NaN | NaN | 0.019131 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 33 | NaN | NaN | NaN | NaN | 0.012616 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 34 | NaN | NaN | NaN | NaN | 0.025286 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 35 | NaN | NaN | NaN | NaN | 0.007545 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 36 | NaN | NaN | NaN | NaN | 0.036684 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 37 | NaN | NaN | NaN | NaN | 0.024785 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 38 | NaN | NaN | NaN | NaN | 0.032609 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 39 | NaN | NaN | NaN | NaN | 0.012466 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
# plt.figure(figsize = (18,8), dpi = 150)
# plt.grid(alpha = 0.5)
# sns.violinplot(data_sorted[2:], palette="Spectral",
# scale='count', inner='quartile',saturation=1, alpha = 0.3)
# sns.swarmplot(data_sorted[2:], palette = 'plasma')
# plt.plot(np.arange(len(data.columns)), data_sorted[0:1].values.T, 'c^', label = 'Provincial Baseline', ms = 15)
# plt.plot(np.arange(len(data.columns)), data_sorted[1:2].values.T, 'm^', label = 'Regional Baseline', ms = 15)
# # plt.yscale('log')
# plt.ylim(-0.002, 0.010)
# plt.title('NTL rate of change of post-disaster resettlement sites in the Philippines (2014-2022)')
# plt.legend()
# plt.figure(figsize = (18,8), dpi = 150)
# plt.grid(alpha = 0.5)
# sns.boxplot(data_sorted[2:], palette="Spectral", notch=True, showcaps=False,
# flierprops={"marker": "x"}, medianprops={"color": "r", "linewidth": 5})
# # sns.swarmplot(data_sorted[2:], palette = 'plasma')
# # for i in range(len(data.columns)):
# # plt.plot([0,len(data.columns)], [0, data_sorted[2:].median()[i]], color = plt.cm.Spectral(i/len(data.columns)))
# plt.plot(np.arange(len(data.columns)), data_sorted[0:1].values.T, 'c^', label = 'Provincial Baseline', ms = 15)
# plt.plot(np.arange(len(data.columns)), data_sorted[1:2].values.T, 'm^', label = 'Regional Baseline', ms = 15)
# plt.title('NTL rate of change of post-disaster resettlement sites in the Philippines (2014-2022)')
# plt.legend()
plt.figure(figsize = (10,7), dpi = 150)
plt.grid(alpha = 0.5)
sns.boxplot(data_sorted[2:], palette="Spectral", orient = 'h',
flierprops={"marker": "x"}, medianprops={"color": "r", "linewidth": 5})
sns.swarmplot(data_sorted[2:], palette = 'gnuplot2', orient = 'h', alpha = 0.3)
# for i in range(len(data.columns)):
# plt.plot([0,len(data.columns)], [0, data_sorted[2:].median()[i]], color = plt.cm.Spectral(i/len(data.columns)))
plt.plot(data_sorted[0:1].values.T, np.arange(len(data.columns)), 'cx', label = 'Provincial Baseline', ms = 10)
plt.plot(data_sorted[1:2].values.T, np.arange(len(data.columns)), 'mx', label = 'Regional Baseline', ms = 10)
# plt.title('NTL rate of change of post-disaster resettlement sites in the Philippines (2014-2022)')
# plt.ylabel("Resettlement Sites")
plt.xlabel("NTL Annual Rate of Change")
plt.legend()
<matplotlib.legend.Legend at 0x7fae19aae050>
index_sort = data[2:].mean().sort_values(ascending = True).index
data_sorted = data[index_sort]
data_sorted
plt.rcParams["figure.dpi"] = 150
d = {'Provincial Baseline': data_sorted[0:1].values[0] - data_sorted[2:].mean().values,
'Regional Baseline': data_sorted[1:2].values[0] - data_sorted[2:].mean().values}
diff = pd.DataFrame(data=d, index=data_sorted.columns)
diff.plot(y=['Provincial Baseline', 'Regional Baseline'], kind = 'barh',
color={'Provincial Baseline': "cyan", 'Regional Baseline': "magenta"},
figsize = (10,6), width=0.6)
plt.grid(alpha = 0.5)
plt.xlabel("Rate of Change difference from the baseline", fontsize = 10)
plt.tight_layout()
plt.show()
# plt.xlim(0,0.08)
# data_sorted[1:2].values[0] - data_sorted[2:].mean().values
data_sorted.columns
Index(['Bulacan', 'Rizal', 'Laguna', 'Samar', 'Leyte', 'Antique', 'Aklan',
'Cebu', 'Catanduanes', 'Biliran', 'Mis Or', 'Lanao DN', 'Albay',
'Davao'],
dtype='object')
plt.bar(np.arange(len(data.columns)),diff['Provincial difference'])
plt.bar(np.arange(len(data.columns)),diff['Provincial difference'])
<BarContainer object of 14 artists>
sns.barplot()
array(['Bulacan', 'Rizal', 'Laguna', 'Samar', 'Leyte', 'Antique', 'Aklan',
'Cebu', 'Catanduanes', 'Biliran', 'Mis Or', 'Lanao DN', 'Albay',
'Davao'], dtype=object)
data_sorted[2:].mean().values
array([0.03156308, 0.03073461, 0.02877841, 0.02621171, 0.02399731,
0.01940291, 0.01754167, 0.01628347, 0.01550688, 0.01549916,
0.01441563, 0.01302667, 0.00939475, 0.00645808])
for i in range(len(data.columns)):
data_sorted[2:].loc(i)
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) /usr/local/lib/python3.10/dist-packages/pandas/core/generic.py in _get_axis_number(cls, axis) 553 try: --> 554 return cls._AXIS_TO_AXIS_NUMBER[axis] 555 except KeyError: KeyError: 2 During handling of the above exception, another exception occurred: ValueError Traceback (most recent call last) <ipython-input-146-3baea1c95da4> in <cell line: 1>() 1 for i in range(len(data.columns)): ----> 2 data_sorted[2:].loc(i) /usr/local/lib/python3.10/dist-packages/pandas/core/indexing.py in __call__(self, axis) 666 667 if axis is not None: --> 668 axis = self.obj._get_axis_number(axis) 669 new_self.axis = axis 670 return new_self /usr/local/lib/python3.10/dist-packages/pandas/core/generic.py in _get_axis_number(cls, axis) 554 return cls._AXIS_TO_AXIS_NUMBER[axis] 555 except KeyError: --> 556 raise ValueError(f"No axis named {axis} for object type {cls.__name__}") 557 558 @final ValueError: No axis named 2 for object type DataFrame
plt.figure(figsize = (10,8), dpi = 150)
plt.grid(alpha = 0.5)
# sns.boxplot(data_sorted[2:], palette="Spectral", orient = 'h',
# flierprops={"marker": "x"}, medianprops={"color": "r", "linewidth": 5})
# # sns.swarmplot(data_sorted[2:], palette = 'gnuplot2', orient = 'h', alpha = 0.3)
# for i in range(len(data.columns)):
# plt.plot([0,len(data.columns)], [0, data_sorted[2:].median()[i]], color = plt.cm.Spectral(i/len(data.columns)))
plt.barh((data_sorted[0:1].values[0] - data_sorted[2:].mean().values, width = 2)
# plt.bar(data_sorted[1:2].values.T - data_sorted[2:].mean(), np.arange(len(data.columns)))
# plt.title('NTL rate of change of post-disaster resettlement sites in the Philippines (2014-2022)')
plt.ylabel("Resettlement Sites")
plt.xlabel("NTL Annual Rate of Change")
plt.legend()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-169-f16e13675570> in <cell line: 9>() 7 # plt.plot([0,len(data.columns)], [0, data_sorted[2:].median()[i]], color = plt.cm.Spectral(i/len(data.columns))) 8 ----> 9 plt.barh(([data_sorted[0:1].values[0] - data_sorted[2:].mean().values]), width = 2) 10 # plt.bar(data_sorted[1:2].values.T - data_sorted[2:].mean(), np.arange(len(data.columns))) 11 # plt.title('NTL rate of change of post-disaster resettlement sites in the Philippines (2014-2022)') /usr/local/lib/python3.10/dist-packages/matplotlib/pyplot.py in barh(y, width, height, left, align, data, **kwargs) 2455 y, width, height=0.8, left=None, *, align='center', 2456 data=None, **kwargs): -> 2457 return gca().barh( 2458 y, width, height=height, left=left, align=align, 2459 **({"data": data} if data is not None else {}), **kwargs) /usr/local/lib/python3.10/dist-packages/matplotlib/axes/_axes.py in barh(self, y, width, height, left, align, data, **kwargs) 2647 """ 2648 kwargs.setdefault('orientation', 'horizontal') -> 2649 patches = self.bar(x=left, height=height, width=width, bottom=y, 2650 align=align, data=data, **kwargs) 2651 return patches /usr/local/lib/python3.10/dist-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs) 1440 def inner(ax, *args, data=None, **kwargs): 1441 if data is None: -> 1442 return func(ax, *map(sanitize_sequence, args), **kwargs) 1443 1444 bound = new_sig.bind(ax, *args, **kwargs) /usr/local/lib/python3.10/dist-packages/matplotlib/axes/_axes.py in bar(self, x, height, width, bottom, align, **kwargs) 2478 hatch, patch_labels) 2479 for l, b, w, h, c, e, lw, htch, lbl in args: -> 2480 r = mpatches.Rectangle( 2481 xy=(l, b), width=w, height=h, 2482 facecolor=c, /usr/local/lib/python3.10/dist-packages/matplotlib/_api/deprecation.py in wrapper(*args, **kwargs) 452 "parameter will become keyword-only %(removal)s.", 453 name=name, obj_type=f"parameter of {func.__name__}()") --> 454 return func(*args, **kwargs) 455 456 # Don't modify *func*'s signature, as boilerplate.py needs it. /usr/local/lib/python3.10/dist-packages/matplotlib/patches.py in __init__(self, xy, width, height, angle, rotation_point, **kwargs) 712 %(Patch:kwdoc)s 713 """ --> 714 super().__init__(**kwargs) 715 self._x0 = xy[0] 716 self._y0 = xy[1] /usr/local/lib/python3.10/dist-packages/matplotlib/_api/deprecation.py in wrapper(*args, **kwargs) 452 "parameter will become keyword-only %(removal)s.", 453 name=name, obj_type=f"parameter of {func.__name__}()") --> 454 return func(*args, **kwargs) 455 456 # Don't modify *func*'s signature, as boilerplate.py needs it. /usr/local/lib/python3.10/dist-packages/matplotlib/patches.py in __init__(self, edgecolor, facecolor, color, linewidth, linestyle, antialiased, hatch, fill, capstyle, joinstyle, **kwargs) 91 self.set_fill(fill) 92 self.set_linestyle(linestyle) ---> 93 self.set_linewidth(linewidth) 94 self.set_antialiased(antialiased) 95 self.set_hatch(hatch) /usr/local/lib/python3.10/dist-packages/matplotlib/patches.py in set_linewidth(self, w) 392 if w is None: 393 w = mpl.rcParams['patch.linewidth'] --> 394 self._linewidth = float(w) 395 self._dash_pattern = mlines._scale_dashes( 396 *self._unscaled_dash_pattern, w) TypeError: only size-1 arrays can be converted to Python scalars
fig = plt.figure(constrained_layout=True, figsize = (12,5), dpi = 150)
spec = gridspec.GridSpec(ncols=2, nrows=2, figure=fig)
ax1 = fig.add_subplot(spec[:,0])
ax1.grid(alpha = 0.5)
for i in range(len(data.columns)):
ax1.plot([2014.55,2022.55], [0, data_sorted[2:].median()[i]], lw = 3,
color = 'k')
for i in range(len(data.columns)):
ax1.plot([2014.5,2022.5], [0, data_sorted[2:].median()[i]], lw = 3,
color = plt.cm.Spectral(i/len(data.columns)), label = str(data_sorted.columns[i]))
ax1.set_ylabel('NTL Rate of Change')
fig.legend(fontsize=10, ncol = 7, bbox_to_anchor= (0, 1.01, 2.1, 0.2), loc="lower left",
bbox_transform=ax1.transAxes, fancybox = True, mode='expand')
ax2 = fig.add_subplot(spec[0,1])
ax2.set_ylabel('Difference from Provincial NTL')
sns.barplot(data_sorted[0:1]-data_sorted[2:].median(), palette="Spectral")
ax2.grid(alpha = 0.5)
ax2.set_ylim(0,0.005)
plt.xticks([])
ax3 = fig.add_subplot(spec[1,1])
ax3.set_ylabel('Difference from Regional NTL')
sns.barplot(data_sorted[1:2]-data_sorted[2:].median(), palette="Spectral")
ax3.grid(alpha = 0.5)
ax3.set_ylim(0,0.005)
plt.xticks([])
plt.tight_layout()
plt.show()
Leyte_df['Seasonality']
| Leyte Province | Region VIII | Kawayan Ville | Villa Diana | Ridge View Park | New Hope Village | Knightsridge Residences | Villa Sofia | North Hill Arbours | Salvacion Heights | ... | Villa Soledad | Virlyn Heights Subdivision | Tabango Ville | Dulag Town Ville | Barugo Town Ville | Villa Verde | Capoocan Village | Dagami Town Ville | Sta. Fe Village Site 1 | Cool Spring Residences | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| date | |||||||||||||||||||||
| 2014-01-01 | -0.103132 | -0.098371 | 0.020012 | 0.027763 | 0.031110 | -0.004142 | 0.041233 | 0.018104 | 0.000407 | 0.017866 | ... | 0.113296 | 0.050744 | 0.059906 | 0.103575 | 0.078341 | 0.132085 | 0.057732 | 0.101062 | 0.034075 | 0.099044 |
| 2014-02-01 | -0.041838 | -0.038405 | 0.012527 | -0.039290 | -0.013462 | -0.038194 | -0.027929 | -0.024743 | -0.013678 | -0.002433 | ... | -0.050066 | 0.007425 | -0.005925 | 0.023387 | 0.055096 | -0.029619 | -0.084467 | 0.037515 | -0.026614 | -0.001270 |
| 2014-03-01 | 0.059328 | 0.042988 | -0.181156 | -0.148180 | -0.146681 | -0.153052 | -0.152547 | -0.172574 | -0.173536 | -0.089915 | ... | -0.054252 | -0.103459 | -0.103440 | -0.090509 | -0.138855 | -0.079634 | -0.083616 | -0.086136 | -0.138984 | -0.097518 |
| 2014-04-01 | 0.144660 | 0.132693 | -0.171002 | -0.211562 | -0.231677 | -0.217995 | -0.206950 | -0.173412 | -0.169973 | -0.227916 | ... | -0.164616 | -0.137074 | -0.167036 | -0.163917 | -0.137877 | -0.134742 | -0.252763 | -0.161639 | -0.170366 | -0.076188 |
| 2014-05-01 | 0.124580 | 0.106606 | 0.295289 | 0.397323 | 0.331590 | 0.354056 | 0.277385 | 0.399490 | 0.301009 | 0.262256 | ... | 0.189255 | 0.332070 | 0.237063 | -0.022846 | -0.098588 | -0.024973 | 0.259477 | 0.028918 | 0.263712 | 0.213953 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2022-07-01 | -0.024151 | -0.014741 | 0.600297 | 0.604459 | 0.830588 | 0.878136 | 0.707635 | 0.594835 | 0.648098 | 0.742986 | ... | 0.109342 | 0.236655 | 0.296021 | 0.127160 | 0.704305 | 0.423533 | 0.608077 | 0.605084 | 0.677630 | 0.233269 |
| 2022-08-01 | 0.027401 | 0.030899 | -0.030288 | -0.040270 | -0.079603 | -0.085427 | -0.023550 | -0.034799 | -0.039374 | -0.055678 | ... | -0.038089 | 0.025407 | 0.073328 | -0.029172 | -0.048232 | 0.044926 | 0.013724 | -0.026534 | -0.029986 | 0.097171 |
| 2022-09-01 | 0.008095 | 0.007868 | -0.155892 | -0.143988 | -0.153193 | -0.150404 | -0.165988 | -0.143838 | -0.147396 | -0.124520 | ... | -0.068896 | -0.046771 | -0.078371 | -0.063162 | -0.071228 | -0.070125 | -0.124132 | -0.101538 | -0.116430 | -0.100760 |
| 2022-10-01 | -0.066057 | -0.030445 | -0.064588 | -0.067525 | -0.104706 | -0.096208 | -0.079508 | -0.054159 | -0.061379 | -0.076677 | ... | 0.020772 | -0.020538 | -0.043537 | 0.014142 | -0.066395 | -0.019687 | -0.053299 | -0.011152 | -0.068218 | -0.058186 |
| 2022-11-01 | -0.081935 | -0.078388 | -0.059587 | -0.068058 | -0.117588 | -0.108914 | -0.072068 | -0.054736 | -0.059380 | -0.076449 | ... | 0.019688 | -0.049045 | -0.023298 | 0.048909 | -0.031155 | -0.030454 | -0.052250 | -0.068668 | -0.072077 | -0.056786 |
107 rows × 40 columns
plt.figure(dpi = 150, figsize = (18,6))
plt.grid(alpha = 0.5)
seasonal = Leyte_df['Seasonality'][:12].rename(index=lambda x: x.strftime('%B'))
ormalized_seasonal=2*((seasonal-seasonal.min())/(seasonal.max()-seasonal.min())-0.5)
plt.plot(seasonal)
plt.show()
plt.figure(dpi = 150, figsize = (25,10))
sns.heatmap(seasonal, cmap = 'RdYlBu_r', annot=True, fmt= '0.1f',)
plt.ylabel('Month')
plt.show()
plt.figure(dpi = 150, figsize = (25,10))
sns.heatmap(normalized_seasonal, cmap = 'RdYlBu_r', annot=True, fmt= '0.1f',)
plt.ylabel('Month')
plt.show()
<Axes: ylabel='date'>
def seasonal_variation(df):
seasonal = df['Seasonality'][:12].rename(index=lambda x: x.strftime('%B'))
normalized_seasonal=2*((seasonal-seasonal.min())/(seasonal.max()-seasonal.min())-0.5)
# plt.figure(dpi = 150, figsize = (25,10))
# sns.heatmap(seasonal, cmap = 'RdYlBu_r', annot=True, fmt= '0.1f',)
# plt.ylabel('Month')
# plt.show()
plt.figure(dpi = 150, figsize = (25,10))
sns.heatmap(normalized_seasonal, cmap = 'RdYlBu_r', annot=True, fmt= '0.1f',)
plt.ylabel('Month')
plt.show()
seasonal_variation(Cebu_df)
seasonal_variation(Samar_df)
seasonal_variation(MisOr_df)
seasonal = Leyte_df['Seasonality'][:12].rename(index=lambda x: x.strftime('%b'))
normalized_seasonal=(seasonal-seasonal.min())/(seasonal.max()-seasonal.min())
normalized_seasonal
| Leyte Province | Region VIII | Kawayan Ville | Villa Diana | Ridge View Park | New Hope Village | Knightsridge Residences | Villa Sofia | North Hill Arbours | Salvacion Heights | ... | Villa Soledad | Virlyn Heights Subdivision | Tabango Ville | Dulag Town Ville | Barugo Town Ville | Villa Verde | Capoocan Village | Dagami Town Ville | Sta. Fe Village Site 1 | Cool Spring Residences | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| date | |||||||||||||||||||||
| Jan | 0.000000 | 0.000000 | 0.370582 | 0.384492 | 0.316073 | 0.292757 | 0.355136 | 0.373755 | 0.339204 | 0.320262 | ... | 0.816847 | 0.550886 | 0.592903 | 0.918974 | 0.370234 | 0.584083 | 0.407595 | 0.438261 | 0.355094 | 0.737811 |
| Feb | 0.247360 | 0.259519 | 0.362464 | 0.312926 | 0.277943 | 0.265461 | 0.288208 | 0.327229 | 0.324834 | 0.301233 | ... | 0.422947 | 0.481730 | 0.479399 | 0.643488 | 0.346848 | 0.353321 | 0.254528 | 0.367437 | 0.294278 | 0.541864 |
| Mar | 0.655633 | 0.611774 | 0.152381 | 0.196708 | 0.163979 | 0.173389 | 0.167618 | 0.166705 | 0.161741 | 0.219226 | ... | 0.412852 | 0.304712 | 0.311268 | 0.252195 | 0.151719 | 0.281946 | 0.255444 | 0.229627 | 0.181672 | 0.353857 |
| Apr | 1.000000 | 1.000000 | 0.163396 | 0.129060 | 0.091268 | 0.121330 | 0.114973 | 0.165796 | 0.165376 | 0.089863 | ... | 0.146741 | 0.251049 | 0.201619 | 0.000000 | 0.152702 | 0.203303 | 0.073370 | 0.145477 | 0.150224 | 0.395523 |
| May | 0.918965 | 0.887100 | 0.669167 | 0.778924 | 0.573124 | 0.579893 | 0.583655 | 0.787884 | 0.645888 | 0.549357 | ... | 1.000000 | 1.000000 | 0.898349 | 0.484652 | 0.192230 | 0.359951 | 0.624759 | 0.357855 | 0.585213 | 0.962269 |
| Jun | 0.580729 | 0.565601 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.000000 | 0.513748 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| Jul | 0.318739 | 0.361931 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | ... | 0.807313 | 0.847678 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 |
| Aug | 0.526785 | 0.559454 | 0.316024 | 0.311880 | 0.221362 | 0.227598 | 0.292447 | 0.316310 | 0.298617 | 0.251321 | ... | 0.451826 | 0.510436 | 0.616043 | 0.462920 | 0.242892 | 0.459701 | 0.360224 | 0.296053 | 0.290899 | 0.734153 |
| Sep | 0.448872 | 0.459780 | 0.179785 | 0.201182 | 0.158408 | 0.175512 | 0.154612 | 0.197908 | 0.188410 | 0.186787 | ... | 0.377544 | 0.395209 | 0.354491 | 0.346148 | 0.219756 | 0.295516 | 0.211832 | 0.212460 | 0.204273 | 0.347525 |
| Oct | 0.149622 | 0.293970 | 0.278819 | 0.282791 | 0.199887 | 0.218956 | 0.238297 | 0.295288 | 0.276168 | 0.231636 | ... | 0.593752 | 0.437089 | 0.414549 | 0.611724 | 0.224619 | 0.367494 | 0.288078 | 0.313197 | 0.252586 | 0.430686 |
| Nov | 0.085545 | 0.086482 | 0.284243 | 0.282222 | 0.188867 | 0.208770 | 0.245497 | 0.294660 | 0.278207 | 0.231850 | ... | 0.591139 | 0.391580 | 0.449445 | 0.731166 | 0.260073 | 0.352129 | 0.289207 | 0.249094 | 0.248719 | 0.433422 |
| Dec | 0.062208 | 0.023140 | 0.409651 | 0.378140 | 0.282604 | 0.289262 | 0.342380 | 0.323616 | 0.387017 | 0.260633 | ... | 0.903030 | 0.468146 | 0.557310 | 0.792668 | 0.335929 | 0.489624 | 0.380371 | 0.298050 | 0.288412 | 0.595018 |
12 rows × 40 columns
lowerLimit = 0
pal = list(sns.color_palette(palette='hsv', n_colors=len(seasonal)).as_hex())
plt.figure(figsize=(8,8))
ax = plt.subplot(111, polar=True)
heights = normalized_seasonal[normalized_seasonal.columns[2:]].mean(axis=1)
width = 2*np.pi / 12
indexes = list(range(1, len(seasonal.index)+1))
angles = [element * width for element in indexes]
ax.plot(angles, normalized_seasonal.iloc[:, 0], 'c--', label = 'Provincial Baseline')
ax.plot(angles, normalized_seasonal.iloc[:, 1], 'm--', label = 'Regional Baseline')
bars = ax.bar(x=angles, height=heights, width=width, bottom=lowerLimit,
linewidth=1, edgecolor="white", color=plt.cm.RdYlBu_r(heights)) #color = pal)#
labelPadding = 0.1
for bar, angle, height, label in zip(bars, angles, heights, seasonal.index):
rotation = np.rad2deg(angle)
alignment = ""
if angle >= np.pi/2 and angle < 3*np.pi/2:
alignment = "right"
rotation = rotation + 180
else:
alignment = "left"
ax.text(x=angle, y = 1.1,#y=lowerLimit + bar.get_height() + labelPadding,
s=label, ha=alignment, va='center') #rotation=rotation,
# rotation_mode="anchor")
ax.set_thetagrids([], labels=[])
ax.legend()
<matplotlib.legend.Legend at 0x7fe2abd34430>
def get_df_name(df):
name =[x for x in globals() if globals()[x] is df][0]
return str(name[:-3])
def seasonal_circle(df):
seasonal = df['Seasonality'][:12].rename(index=lambda x: x.strftime('%b'))
normalized_seasonal=(seasonal-seasonal.min())/(seasonal.max()-seasonal.min())
lowerLimit = 0
pal = list(sns.color_palette(palette='hsv', n_colors=len(seasonal)).as_hex())
plt.figure(figsize=(5,4), dpi = 150)
ax = plt.subplot(111, polar=True)
heights = normalized_seasonal[normalized_seasonal.columns[2:]].mean(axis=1)
width = 2*np.pi / 12
indexes = list(range(1, len(seasonal.index)+1))
angles = [element * width for element in indexes]
ax.plot(angles, normalized_seasonal.iloc[:, 0], 'co--', label = 'Provincial Baseline', lw = 2)
ax.plot(angles, normalized_seasonal.iloc[:, 1], 'mo--', label = 'Regional Baseline', lw = 2)
# ax.plot(angles, heights, 'ks-', label = 'Resettlement Sites', lw = 3, alpha = 0.5)
bars = ax.bar(x=angles, height=heights, width=width, bottom=lowerLimit, label = 'Resettlement Sites',
linewidth=1, edgecolor="white", color=plt.cm.RdYlBu_r(heights)) #color = pal)#
labelPadding = 0.1
for bar, angle, height, label in zip(bars, angles, heights, seasonal.index):
rotation = np.rad2deg(angle)
alignment = ""
if angle >= np.pi/2 and angle < 3*np.pi/2:
alignment = "right"
rotation = rotation + 180
else:
alignment = "left"
ax.text(x=angle, y = 1.1,#y=lowerLimit + bar.get_height() + labelPadding,
s=label, ha=alignment, va='center') #rotation=rotation,
# rotation_mode="anchor")
ax.set_thetagrids([], labels=[])
ax.legend(fontsize = 8, title = get_df_name(df), bbox_to_anchor= (0, 0.09, 0.95, 0.2), fancybox = True)
return ax
seasonal_circle(Leyte_df)
<PolarAxes: >
seasonal_circle(Leyte_df)
<PolarAxes: >
seasonal_circle(Samar_df)
<PolarAxes: >
seasonal_circle(Cebu_df)
<PolarAxes: >
seasonal_circle(Biliran_df)
<PolarAxes: >
seasonal_circle(MisOr_df)
<PolarAxes: >
seasonal_circle(Lanao_df)
<PolarAxes: >
seasonal_circle(Davao_df)
<PolarAxes: >
seasonal_circle(Aklan_df)
<PolarAxes: >
seasonal_circle(Antique_df)
<PolarAxes: >
seasonal_circle(Albay_df)
<PolarAxes: >
seasonal_circle(Catanduanes_df)
<PolarAxes: >
seasonal_circle(Laguna_df)
<PolarAxes: >
seasonal_circle(Bulacan_df)
<PolarAxes: >
seasonal_circle(Rizal_df)
<PolarAxes: >